如何在Firestore Firebase中检索子集合

时间:2018-07-05 07:45:27

标签: android firebase google-cloud-firestore

我有一个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 :(得分: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);
                 }
             });