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

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

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

问题

  1. ![图片描述](链接)
  2. 我无法插入图片,您可以访问上面的链接或阅读下面的描述。
  3. 这是Firestore中的文档内容 -

个人资料

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

  1. 我通过Firebase控制台手动添加了数据。现在我想将数据检索到应用程序中创建的自定义Java对象中。
  2. 此对象专为该文档定制。
  3. 测试类 -
  4. ```java
  5. package com.example.firebase;
  6. public class test {
  7. private String name;
  8. private int DOB;
  9. private int age;
  10. private String membership;
  11. private String trainer_name;
  12. public test(String name, int DOB, int age, String membership, String trainer_name) {
  13. this.name = name;
  14. this.DOB = DOB;
  15. this.age = age;
  16. this.membership = membership;
  17. this.trainer_name = trainer_name;
  18. }
  19. public test(){
  20. //无参构造函数
  21. }
  22. // 省略了getter和setter方法
  23. }

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

  1. //自定义对象
  2. db.collection("users").document("Saheel").collection("profile")
  3. .document("profile").get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
  4. @Override
  5. public void onSuccess(DocumentSnapshot documentSnapshot) {
  6. test test = documentSnapshot.toObject(com.example.firebase.test.class);
  7. name_t.setText(test.getName());
  8. age_t.setText(String.valueOf(test.getAge())); // 将整数转换为字符串
  9. dob_t.setText(String.valueOf(test.getDOB())); // 将整数转换为字符串
  10. membership_t.setText(test.getMembership());
  11. trainer_t.setText(test.getTrainer_name());
  12. }
  13. });

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

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

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

  1. <details>
  2. <summary>英文:</summary>
  3. [![enter image description here][1]][1]
  4. I cant insert the image, you can visit the link above or read the description below.
  5. This is how the Document in the Firestore is present -

Profile

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

  1. 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.
  2. This object is tailor made for the document.
  3. Test class -
  4. ```java
  5. package com.example.firebase;
  6. public class test {
  7. private String name;
  8. private int DOB;
  9. private int age;
  10. private String membership;
  11. private String trainer_name;
  12. public test(String name, int DOB, int age, String membership, String trainer_name) {
  13. this.name = name;
  14. this.DOB = DOB;
  15. this.age = age;
  16. this.membership = membership;
  17. this.trainer_name = trainer_name;
  18. }
  19. public test(){
  20. //no-argument constructor
  21. }
  22. public String getName() {
  23. return name;
  24. }
  25. public void setName(String name) {
  26. this.name = name;
  27. }
  28. public int getDOB() {
  29. return DOB;
  30. }
  31. public void setDOB(int DOB) {
  32. this.DOB = DOB;
  33. }
  34. public int getAge() {
  35. return age;
  36. }
  37. public void setAge(int age) {
  38. this.age = age;
  39. }
  40. public String getMembership() {
  41. return membership;
  42. }
  43. public void setMembership(String membership) {
  44. this.membership = membership;
  45. }
  46. public String getTrainer_name() {
  47. return trainer_name;
  48. }
  49. public void setTrainer_name(String trainer_name) {
  50. this.trainer_name = trainer_name;
  51. }
  52. }

The code which requests the data from the database -

  1. //CUSTOM OBJECTS
  2. db.collection(&quot;users&quot;).document(&quot;Saheel&quot;).collection(&quot;profile&quot;)
  3. .document(&quot;profile&quot;).get().addOnSuccessListener(new OnSuccessListener&lt;DocumentSnapshot&gt;() {
  4. @Override
  5. public void onSuccess(DocumentSnapshot documentSnapshot) {
  6. test test =documentSnapshot.toObject(com.example.firebase.test.class);
  7. name_t.setText(test.getName());
  8. age_t.setText(test.getAge());
  9. dob_t.setText(test.getDOB());
  10. membership_t.setText(test.getMembership());
  11. trainer_t.setText(test.getTrainer_name());
  12. }
  13. });

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

  1. 2020-10-07 20:50:30.366 9016-9016/com.example.firebase E/AndroidRuntime: FATAL EXCEPTION: main
  2. Process: com.example.firebase, PID: 9016
  3. android.content.res.Resources$NotFoundException: String resource ID #0x13
  4. at android.content.res.Resources.getText(Resources.java:444)
  5. at android.widget.TextView.setText(TextView.java:6412)
  6. at com.example.firebase.MainActivity$1.onSuccess(MainActivity.java:81)
  7. at com.example.firebase.MainActivity$1.onSuccess(MainActivity.java:76)
  8. at com.google.android.gms.tasks.zzn.run(Unknown Source:4)
  9. at android.os.Handler.handleCallback(Handler.java:938)
  10. at android.os.Handler.dispatchMessage(Handler.java:99)
  11. at android.os.Looper.loop(Looper.java:223)
  12. at android.app.ActivityThread.main(ActivityThread.java:7656)
  13. at java.lang.reflect.Method.invoke(Native Method)
  14. at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
  15. 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中:

  1. dob_t.setText(test.getDOB());

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

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

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

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

  1. 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:

  1. 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:

确定