【问题标题】:Can't retrive data from firebase firestore and show it in View Text in android studio无法从 firebase firestore 检索数据并将其显示在 android studio 的 View Text 中
【发布时间】:2021-11-12 21:58:14
【问题描述】:

数据已保存在 Firestore 中,但我无法获取。此外,创建的列表大小仍然等于 null。 我知道我想从existUserProfileModelList 而不是从existUserProfileModel 获取数据,但我不知道该怎么做。这是我的代码:

这是我的数据库:

public class UserProfileActivity extends AppCompatActivity {

 // created List from User Profile Model

private List<ExistUserProfileModel> existUserProfileModelList;
// define some Edit texts
private EditText profile_phone_ET, profile_FullName_ET, profile_Address_ET;
// define FireStore  
private FirebaseFirestore firestore;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_profile);

    profile_phone_ET = findViewById(R.id.profile_Phone);
    profile_FullName_ET = findViewById(R.id.profile_FullName);
    profile_Address_ET = findViewById(R.id.profile_Address);
    firestore = FirebaseFirestore.getInstance();
    existUserProfileModelList = new ArrayList<>();

   firestore.collection("UserData").document().collection(profilePhone)
                                .get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                            @Override
                            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                                if (task.isSuccessful()) {
                                    for (DocumentSnapshot document : task.getResult()) {
                                        ExistUserProfileModel existUserProfileModel = document.toObject(ExistUserProfileModel.class);
                                        existUserProfileModelList.add(existUserProfileModel);

                                        profile_FullName_ET.setText(existUserProfileModel.getUserName());
                                        profile_Address_ET.setText(existUserProfileModel.getUserAddress());
                                    }
                                        Log.i("Size ", String.valueOf(existUserProfileModelList.size()));
                                }

                                else{
                                       Toast.makeText(UserProfileActivity.this, "Error" + task.getException(), Toast.LENGTH_SHORT).show();
                                }
                            }
                        });

【问题讨论】:

  • 什么是null?
  • 我创建了一个名为 ExistUserProfileModel 的模型,然后从中定义了一个数组列表,当我从 firestore 检索数据并通过此数据更新列表时,我得到数组大小仍然为零
  • 您在代码中的哪个位置进行检查?
  • 在 if 条件下的 for 语句之后,但我删除了它。
  • 将其添加到您的问题中以清楚地看到它,还请编辑您的问题并将您的数据库结构添加为屏幕截图。

标签: java android google-cloud-firestore


【解决方案1】:

问题可能出在您的查询中:

firestore.collection("UserData").document().collection(profilePhone).get()

由于您没有在.document() 中传递任何参数,这意味着您在选择它的子文档之前没有选择任何“父”UserData 文档。

正如您在documentation 中看到的那样,您需要选择要通过其 docId 的文档。因此,您的代码中的这个小改动将使其按预期工作:

firestore.collection("UserData").document(docId).collection(profilePhone).get()

注意:您应该如何获得此docId 取决于您的应用程序的早期阶段,如果您没有此可用,您可能需要在此文档中创建一些字段以将其过滤掉或获取所有文档并选择您需要的那个。

【讨论】:

  • 您好@dabosadbosa,欢迎来到 StackOverflow!请记得react to answers for your questions。这样我们就知道答案是否有帮助,其他社区成员也可以从中受益。尝试accept answer,这是您问题的最终解决方案,支持有帮助的答案,并对可以改进或需要额外关注的答案发表评论。祝您住宿愉快!
猜你喜欢
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-29
  • 1970-01-01
  • 2022-01-24
  • 2020-03-28
相关资源
最近更新 更多