如何在Firestore Android中获取集合的根文档字段

时间:2019-02-03 11:10:26

标签: android firebase google-cloud-firestore

我想在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();
            }
        }
    });

1 个答案:

答案 0 :(得分:0)

enter image description here使用此行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