Skip to content Skip to sidebar Skip to footer

How To Query A Collection To Get Specific Documents?

I'll start to explain my issue using an example, I have 2 root collections named Users and Foods. In Users collection, contains documents with User information such as Name, Image,

Solution 1:

Finally, I solve my problem and this is what I wanted :)

enter code here  finalFirebaseFirestorerootRef= FirebaseFirestore.getInstance();
    FirebaseUseruser= FirebaseAuth.getInstance().getCurrentUser();
    if (user!=null){
        StringUSER_ID= user.getUid();

        rootRef.collection("Users").document(USER_ID).collection("Bookmarks")
                .addSnapshotListener(newEventListener<QuerySnapshot>() {
                    @OverridepublicvoidonEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
                        if (error!=null){
                            Log.w(TAG, "Listen failed.", error);
                            return;
                        }
                        List<String> foodlist = newArrayList<>();
                        assert value != null;
                        for (QueryDocumentSnapshot doc : value) {
                            if (doc.get("Bookmark_Document_Id") != null) {


                                foodlist.add(doc.getString("Bookmark_Document_Id"));
                                Log.d(TAG, "Food items are: " + foodlist);
                               
                                Queryquery= rootRef.collection("Recipes").document("Food_Recipe").collection("Powder_Recipe").whereIn("Document_Id",foodlist);
                                PagedList.Configconfig=newPagedList.Config.Builder()
                                        .setEnablePlaceholders(false)
                                        .setPrefetchDistance(2)
                                        .setInitialLoadSizeHint(2)
                                        .setPageSize(3)
                                        .build();

                                FirestorePagingOptions<FoodItems> options = newFirestorePagingOptions.Builder<FoodItems>()
                                        .setQuery(query, config, FoodItems.class)
                                        .build();

                                food_items_recycle_adapter = newFood_Items_Recycle_Adapter(options, progressIndicator,null);
                                recyclerView.setHasFixedSize(true);
                                recyclerView.setLayoutManager(newLinearLayoutManager(getContext()));
                                recyclerView.setAdapter(food_items_recycle_adapter);
                                food_items_recycle_adapter.notifyDataSetChanged();
                                food_items_recycle_adapter.startListening();

                            }else {
                                Log.d(TAG, "onEvent: this is nulll");
                            }


                        }
                    }
                });
    }

Thank you @s_o_m_m_y_e_e, you tried to solve my issue. There are should be many ways to archive this, but this is working as I wanted.

Post a Comment for "How To Query A Collection To Get Specific Documents?"