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, and Age. In foodsCollection, contains documents with Fields such as Food_name, Ingredients, and Image. Now I want to let the users bookmark foods as user wish.Therefore I create one more collection in a user document named Bookmark and saved the Food document's Id in the Bookmark collection's documents. Now I want to get the only Bookmarked Document using saved food document Ids. I ll write my data structure for further understanding.
Foods(Collection) ----1ztEzZLkTp5XOZzsoYRL(Feilds--Name,Image,Ingradents,Document_Id)
----4Q6k0MifwEyoLs8rgZbs(Feilds--Name,Image,Ingradents,Document_Id)
----870BEaDFMkMWTi4VaXmh(Feilds--Name,Image,Ingradents,Document_Id)
[...]
Users(Collection)----User1(Document)----Bookmarks(Collection)----RandomId("Bookmarked_Doc_Id" - 1ztEzZLkTp5XOZzsoYRL)
----RandomId("Bookmarked_Doc_Id" - 870BEaDFMkMWTi4VaXmh)
----User2(Document)----Bookmarks(Collection)----RandomId("Bookmarked_Doc_Id" - 1ztEzZLkTp5XOZzsoYRL)
[...]
Now I want to get the Food collection's document data using Bookmarked_Doc_Id
For example, When User1 clicks bookmark tab, he can see 2 bookmarks with its name,image and ingradents.
How can archive this ?
Edit
rootRef.collection("Users").document(USER_ID).collection("Bookmarks").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()){
for (DocumentSnapshot document : task.getResult()){
if (document.exists()){
String getDocuments = document.get("Bookmark_Document_Id").toString();
loadBookmarks(getDocuments);
Log.d(TAG, "onComplete: "+" "+getDocuments);
}
}
}
}
});
private void loadBookmarks(String getDocuments) {
Query query = FirebaseFirestore.getInstance().collection("Foods").whereEqualTo("Document_Id",getDocuments);
PagedList.Config config = new PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setPrefetchDistance(2)
.setInitialLoadSizeHint(2)
.setPageSize(3)
.build();
FirestorePagingOptions<FoodItems> options = new FirestorePagingOptions.Builder<FoodItems>()
.setQuery(query, config, FoodItems.class)
.build();
food_items_recycle_adapter = new Food_Items_Recycle_Adapter(options, progressIndicator,null);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(food_items_recycle_adapter);
food_items_recycle_adapter.notifyDataSetChanged();
food_items_recycle_adapter.startListening();
}
This code only shows me the latest added bookmark instead of showing all bookmarks.