首页 文章

如何在firestore firebase中检索Sub Collection

提问于
浏览
-3

我有一个Firebase项目,其结构如下

CollectionReference adminList;
adminList = mFirestore.collection("AdminList");
            Map<String,Object> k = new HashMap<>();
            k.put("order",order);
            k.put("status","New Order");
            k.put("hotel_name",username.getText().toString());
            k.put("user_id",mUid);
            k.put("time", "Select Time");
            //adminList.document(order).set(k);
            adminList.document(mUid).collection("User").document(order).set(k);

可以有多个用户使用UserId . 我必须在列表中检索它 .

为此,我使用了以下代码 .

@Override
protected void onStart() {
    super.onStart();

    mFirestore.collection("AdminList").getParent().collection("Users").addSnapshotListener(MainActivity.this, new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {

            orderList.clear();
            assert queryDocumentSnapshots != null;
            List<OrderList> types = queryDocumentSnapshots.toObjects(OrderList.class);
            orderList.addAll(types);

            adapter = new AdminListAdapter(MainActivity.this,orderList);
            recyclerView.setAdapter(adapter);

        }
    });

}

但我需要一种方法来访问子集合及其文档,以便我可以在recyclerview中检索列表 .

enter image description here

enter image description here

1 回答

  • 0

    这样做: -

    firestore.collection("AdminList").document(mUid).collection("User").document(order).addSnapshotListener(this, new EventListener<DocumentSnapshot>() {
                @Override
                public void onEvent(DocumentSnapshot documentSnapshot, FirebaseFirestoreException e)
                {
                    if (documentSnapshot.exists())
                    {
                      types = queryDocumentSnapshots.toObjects(OrderList.class);
                      orderList.addAll(types);
                     }
                 });
    

相关问题