【问题标题】:How to Retrieve Sub Collection in firestore firebase如何在 Firestore Firebase 中检索子集合
【发布时间】:2018-12-13 14:44:49
【问题描述】:

我有一个具有类似结构的 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 中检索列表。

【问题讨论】:

  • 能否请您添加您的数据库结构并指出您想要获取哪些值以及您想要添加到 ArrayList 中?
  • 我已经上传了截图请看
  • 这很好,但请指出您想要获取哪些值以及您想要添加到 ArrayList 中? hotel_nameorder等等,对吗?

标签: android firebase google-cloud-firestore


【解决方案1】:

这样做:-

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);
                 }
             });

【讨论】:

    猜你喜欢
    • 2020-09-01
    • 2022-11-01
    • 2018-07-07
    • 1970-01-01
    • 2019-02-05
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    • 2021-03-24
    相关资源
    最近更新 更多