【问题标题】:how to get root document fields of collection in firestore android如何在firestore android中获取集合的根文档字段
【发布时间】:2020-04-28 21:26:27
【问题描述】:

我想在android firestore中获取根集合的文档字段

我在firestore中的数据库结构是

collection
      documents-->root fields
               collection
                          documents-->fields

现在我想获取根字段。如何在 android firestore 中做到这一点?

firestore.collection("Players").orderBy("deptName").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {

                for (QueryDocumentSnapshot document : task.getResult()) {

                    //model class
                    Example example=document.toObject(Example.class);
                    exampleList.add(example);

                }


                Toast.makeText(getActivity(), ""+exampleList, Toast.LENGTH_LONG).show();

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

【问题讨论】:

  • 您的代码从根 Players 集合中获取文档。这段代码有什么问题?它给出了什么结果?您期望/想要得到什么结果?
  • 这段代码有什么问题?还请添加您的数据库架构的屏幕截图,也请回复@。

标签: android firebase google-cloud-firestore


【解决方案1】:

使用这一行document.getId();

firestore.collection("Players").orderBy("deptName").get()

.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {

                for (QueryDocumentSnapshot document : task.getResult()) {

                    //model class
                    Example example=document.toObject(Example.class);
 example.setDocID(document.getId());
                    exampleList.add(example);

                }


                Toast.makeText(getActivity(), ""+exampleList, Toast.LENGTH_LONG).show();

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

https://firebase.google.com/docs/firestore/query-data/get-data#java_2

【讨论】:

    猜你喜欢
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多