如何直接从Firebase Cloud Firestore 检索数据到自定义的JAVA对象中。

huangapple go评论77阅读模式
英文:

How to Retrieve Data directly from the Firebase Cloud Firestore into a CUSTOM JAVA OBJECT

问题

![图片描述](链接)

我无法插入图片,您可以访问上面的链接或阅读下面的描述。
这是Firestore中的文档内容 -

个人资料

出生日期 23032001
年龄 19
会员 "GOLD"
姓名 "Saheel"
训练师姓名 "trainer1"

我通过Firebase控制台手动添加了数据。现在我想将数据检索到应用程序中创建的自定义Java对象中。
此对象专为该文档定制。
测试类 -

```java
package com.example.firebase;

public class test {
    private String name;
    private int DOB;
    private int age;
    private String membership;
    private String trainer_name;

    public test(String name, int DOB, int age, String membership, String trainer_name) {
        this.name = name;
        this.DOB = DOB;
        this.age = age;
        this.membership = membership;
        this.trainer_name = trainer_name;
    }

    public test(){
        //无参构造函数
    }

    // 省略了getter和setter方法
}

从数据库请求数据的代码 -

//自定义对象

db.collection("users").document("Saheel").collection("profile")
        .document("profile").get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
    @Override
    public void onSuccess(DocumentSnapshot documentSnapshot) {
        test test = documentSnapshot.toObject(com.example.firebase.test.class);
        name_t.setText(test.getName());
        age_t.setText(String.valueOf(test.getAge()));  // 将整数转换为字符串
        dob_t.setText(String.valueOf(test.getDOB()));  // 将整数转换为字符串
        membership_t.setText(test.getMembership());
        trainer_t.setText(test.getTrainer_name());
    }
});

应用程序会立即崩溃,并且Logcat显示以下错误 -

2020-10-07 20:50:30.366 9016-9016/com.example.firebase E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.firebase, PID: 9016
    android.content.res.Resources$NotFoundException: String resource ID #0x13
        at android.content.res.Resources.getText(Resources.java:444)
        at android.widget.TextView.setText(TextView.java:6412)
        at com.example.firebase.MainActivity$1.onSuccess(MainActivity.java:81)
        at com.example.firebase.MainActivity$1.onSuccess(MainActivity.java:76)
        at com.google.android.gms.tasks.zzn.run(Unknown Source:4)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

我是否遵循了正确的流程?在Firestore中是否可能执行此操作?


<details>
<summary>英文:</summary>

[![enter image description here][1]][1]

I cant insert the image, you can visit the link above or read the description below.
This is how the Document in the Firestore is present -

Profile

DOB 23032001
age 19
membership "GOLD"
name "Saheel"
trainer_name "trainer1"

I added the data manually through the Firebase console. Now I want to retrieve the data into an Custom Java Object which i have created in the application.
This object is tailor made for the document.
Test class -

```java
package com.example.firebase;

public class test {
    private String name;
    private int DOB;
    private int age;
    private String membership;
    private String trainer_name;

    public test(String name, int DOB, int age, String membership, String trainer_name) {
        this.name = name;
        this.DOB = DOB;
        this.age = age;
        this.membership = membership;
        this.trainer_name = trainer_name;
    }

    public test(){
        //no-argument constructor
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getDOB() {
        return DOB;
    }

    public void setDOB(int DOB) {
        this.DOB = DOB;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getMembership() {
        return membership;
    }

    public void setMembership(String membership) {
        this.membership = membership;
    }

    public String getTrainer_name() {
        return trainer_name;
    }

    public void setTrainer_name(String trainer_name) {
        this.trainer_name = trainer_name;
    }
}

The code which requests the data from the database -

//CUSTOM OBJECTS

        db.collection(&quot;users&quot;).document(&quot;Saheel&quot;).collection(&quot;profile&quot;)
                .document(&quot;profile&quot;).get().addOnSuccessListener(new OnSuccessListener&lt;DocumentSnapshot&gt;() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                test test =documentSnapshot.toObject(com.example.firebase.test.class);
                name_t.setText(test.getName());
                age_t.setText(test.getAge());
                dob_t.setText(test.getDOB());
                membership_t.setText(test.getMembership());
                trainer_t.setText(test.getTrainer_name());
            }
        });

The Application crashes immediately and the logcat shows the below error -

2020-10-07 20:50:30.366 9016-9016/com.example.firebase E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.firebase, PID: 9016
    android.content.res.Resources$NotFoundException: String resource ID #0x13
        at android.content.res.Resources.getText(Resources.java:444)
        at android.widget.TextView.setText(TextView.java:6412)
        at com.example.firebase.MainActivity$1.onSuccess(MainActivity.java:81)
        at com.example.firebase.MainActivity$1.onSuccess(MainActivity.java:76)
        at com.google.android.gms.tasks.zzn.run(Unknown Source:4)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

Am I following the right procedure? Is it possible to do this Operation in Firestore?

答案1

得分: 1

由于text.getDOB()返回一个整数,此代码尝试将整数放入TextView中:

dob_t.setText(test.getDOB());

当您使用整数调用setText时,Android会将该整数视为具有给定数字ID的字符串资源。由于Android找不到该字符串资源,因此会导致应用崩溃。

如果您需要在TextView中显示数字作为字符串,您需要首先将其转换为字符串:

dob_t.setText(Integer.toString(test.getDOB()));
英文:

Since text.getDOB() returns an int, this code is trying to put an integer into a TextView:

dob_t.setText(test.getDOB());

When you call setText with an integer, Android takes that integer to be a string resource with the given numeric ID. Since Android can't find that string resource, it crashes.

If you need to show a number as a string in a TextView, you will need to convert it to a string first:

dob_t.setText(Integer.toString(test.getDOB()));

huangapple
  • 本文由 发表于 2020年10月7日 23:25:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/64247277.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定