使用Firestore从一个集合中获取产品并从另一个集合中获取产品费率

时间:2019-07-03 11:21:11

标签: java android google-cloud-firestore

我有两个收藏夹: 1. RetailerRegister, 2. CustomerRetailerRate。

第一个是零售商的集合,第二个是客户费率,所以现在我获得了所有零售商列表,现在我希望从另一个名为“ CustomerRetailerRate”的集合中获取费率。

我尝试遵循该代码,但仍然收到错误:“ java.lang.IndexOutOfBoundsException:索引:0,大小:0”。我认为在我的recyclerview适配器中可能有问题,但是没有问题。实际上,在“ CustomerRetailerRate”集合中只有2个条目,在“ RetailerRegister”集合中只有19个条目,因此我从“ RetailerRegister”获得费率数据,但没有获得空值。我还看到了此解决方案Click Here

Query query = db.collection("RetailerRegister").whereEqualTo("Area", "Satellite").limit(limit);

        query.get().addOnCompleteListener(task -> {
            try {
                if (task.isSuccessful()) {

                    for (QueryDocumentSnapshot document : Objects.requireNonNull(task.getResult())) {
                        IsOffer = Boolean.valueOf(String.valueOf(document.get("IsOffer")));
                        StoreName.add(String.valueOf(document.get("RBusinessName")));

                        Query dataStoreRate = db.collection("CustomerRetailerRate").whereEqualTo("RUnicID", document.get("RUnicID"));
                        dataStoreRate.get().addOnCompleteListener(tasks -> {
                            try {
                                if (tasks.isSuccessful()) {
                                    for (QueryDocumentSnapshot documents : Objects.requireNonNull(tasks.getResult())) {
                                        Log.i(TAG, "loadNextData: " + documents.get("RatePoints"));
                                        StoreRate.add(String.valueOf(documents.get("RatePoints")));
                                    }
                                }
                            } catch (Exception ex) {
                                StoreRate.add("0.0");
                            }
                        }).addOnFailureListener(e -> {
                            StoreRate.add("0.0");
                        });


                        StoreDelivery.add(String.valueOf(document.get("Deliver In")));
                        StoreOfferAmt.add(IsOffer);
                        StoreOfferText.add(IsOffer);
                        txtStoreOfferAmt.add(String.valueOf(document.get("OfferAmt")));
                        txtStoreOfferText.add(String.valueOf(document.get("OfferContent")));
                    }
                    storeListAdapter.notifyDataSetChanged();
                    lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);
                    Toast.makeText(getActivity(), "First Page", Toast.LENGTH_SHORT).show();
                } else {
                    Log.d(TAG, "Error getting documents: ", task.getException());

                    Toast.makeText(getActivity(), "No Grocery Store Found At Your Locations!!", Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
                Toast.makeText(getActivity(), "No Grocery Store Found At Your Locations!!", Toast.LENGTH_SHORT).show();
            }
        });

0 个答案:

没有答案