Firebase Firestore的其他收集文档中的文档的子收集

时间:2019-01-24 11:15:09

标签: android firebase google-cloud-firestore

我有三个收藏品。

第一个用户集合是该客户集合内的客户集合具有taskcollection。

现在我得到了第一个用户,然后得到了客户,并且每个客户收集文档的更改都获得了具有限制条件的任务收集日期。

然后再次获取其通过日期值的taskcollection。

但是我的问题是,如果文档被更改,那么它每次都会重新设置日期,因此数据会多次显示..

下面是我的代码:

firestoreDB.collection(confing.USER_COLLECTION)
                .document(UserId)
                .collection(confing.Customer_COLLECTION)
                .addSnapshotListener(getActivity(), new EventListener<QuerySnapshot>() {
                    @Override
                    public void onEvent(final QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
                        if (e != null) {
                            return;
                        }
                        categorylist.clear();
                        filterlist.clear();
                        final ArrayList<Date> values = new ArrayList<Date>();
                        for (final DocumentChange documentChange : documentSnapshots.getDocumentChanges()) {
                            documentChange.getDocument()
                                    .getReference()
                                    .collection(confing.TASK_COLLECTION)
                                    .whereEqualTo("TaskStatus", "Pending")
                                    .orderBy("TaskDate", Query.Direction.DESCENDING)
                                    .limit(1)
                                    .addSnapshotListener(getActivity(), new EventListener<QuerySnapshot>() {
                                        @Override
                                        public void onEvent(@javax.annotation.Nullable QuerySnapshot queryDocumentSnapshots, @javax.annotation.Nullable FirebaseFirestoreException e) {
                                            if (e != null) {
                                                return;
                                            }

                                            for (DocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
                                                final Category_Model categorydata = new Category_Model();
                                                Date value = (documentSnapshot.getDate("TaskDate"));
                                               /* if (!values.contains(value)) {
                                                    values.add(value);
                                                }*/
                                                categorydata.setTaskdate(value);
                                                categorydata.setStatus(documentSnapshot.getString("TaskStatus"));
                                                final ArrayList<Task_datatype> categorytasklist = new ArrayList<Task_datatype>();
                                                documentChange.getDocument()
                                                        .getReference()
                                                        .collection(confing.TASK_COLLECTION)
                                                        .whereEqualTo("TaskDate", documentSnapshot.getDate("TaskDate"))
                                                        .whereEqualTo("TaskStatus", documentSnapshot.getString("TaskStatus"))
                                                        .addSnapshotListener(getActivity(), new EventListener<QuerySnapshot>() {
                                                            @Override
                                                            public void onEvent(@javax.annotation.Nullable QuerySnapshot queryDocumentSnapshots, @javax.annotation.Nullable FirebaseFirestoreException e) {
                                                                if (e != null) {
                                                                    return;
                                                                }
                                                                for (DocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
                                                                    final Task_datatype task_datatype = new Task_datatype();
                                                                    task_datatype.setTaskDocId(documentSnapshot.getId());
                                                                    task_datatype.setType(documentSnapshot.getString("Type"));
                                                                    task_datatype.setTaskDate(inputFormatter1.format(documentSnapshot.getDate("TaskDate")));
                                                                    task_datatype.setTaskDescription(documentSnapshot.getString("TaskDescription"));
                                                                    task_datatype.setTaskTime(documentSnapshot.getString("TaskTime"));
                                                                    task_datatype.setTaskStatus(documentSnapshot.getString("TaskStatus"));
                                                                    task_datatype.setCustomername(documentChange.getDocument().getString("CustomerName"));
                                                                    task_datatype.setCustomermobileno(documentChange.getDocument().getString("MobileNo1"));
                                                                    task_datatype.setCustomerDocId(documentChange.getDocument().getId());
                                                                    categorytasklist.add(task_datatype);
                                                                }
                                                                categorydata.setCategory_task_modelArrayList(categorytasklist);
                                                                categorylist.add(categorydata);
                                                                filterlist.add(categorydata);
                                                                if (categorylist.size() > 0 && filterlist.size() > 0) {
                                                                    monthTaskBinding.noimage.setVisibility(View.GONE);
                                                                    category_adapter = new DoneTask_Adapter(filterlist, getActivity(), firestoreDB, monthTaskBinding.swipereferesh,progressDialog);
                                                                    monthTaskBinding.monthTasklist.setLayoutManager(new LinearLayoutManager(getActivity()));
                                                                    monthTaskBinding.monthTasklist.setAdapter(category_adapter);
                                                                } else {
                                                                    monthTaskBinding.noimage.setVisibility(View.VISIBLE);
                                                                }
                                                            }
                                                        });
                                            }

                                        }
                                    });

                        }


                        // monthTaskBinding.swipereferesh.setRefreshing(false);
                    }
                });

0 个答案:

没有答案