0

I am trying to code a Search function in my App. Data is fetched from Firebase Database.

This is what the DB structure looks like at the Root Node :

enter image description here

After Expanding the items node, this is the DB structure :

enter image description here

I am trying to get the name node which is inside the itemsnode. How can i do that ?

So, far I have tried this:

DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("HomeItems");
databaseReference.keepSynced(true);

options = new FirebaseRecyclerOptions.Builder<HomeModel>()
        .setQuery(FirebaseDatabase.getInstance().getReference("HomeItems").orderByChild("name").startAt(String.valueOf(editable)).endAt(editable + "\uf8ff"), HomeModel.class)
        .build();

Log.e("afterTextChanged: ", String.valueOf(options.getSnapshots()));

recyclerView.setLayoutManager(new GridLayoutManager(SearchActivity.this, 3));
homeAdapter = new HomeAdapter(options, SearchActivity.this);
homeAdapter.startListening();

recyclerView.setAdapter(homeAdapter);

The current Output gives the values of the Direct node of name . But, how do I get the same for the nodes which are inside the items node?

Sainita
  • 332
  • 1
  • 4
  • 16
  • What is the value of `ref` in `FirebaseDatabase.getInstance().getReference(ref)`? Can you show how that variable is initialized? – Frank van Puffelen Sep 19 '21 at 13:57
  • ref is "HomeItems" , i change the code – Sainita Sep 19 '21 at 14:20
  • @FrankvanPuffelen , Pls see the updated code – Sainita Sep 19 '21 at 14:34
  • Sorry, but nothing was changed. We need to know how `ref` in `getReference(ref)` is initialized. – Frank van Puffelen Sep 19 '21 at 15:01
  • @FrankvanPuffelen , pls check now – Sainita Sep 19 '21 at 15:47
  • 2
    Firebase queries work on a flat list of child nodes. The value to order/filter on must be at a fixed path under each direct child. So you can't query across all of `HomeItems` for any of them that has a certain `name` in one of its `items`. You can query across all `HomeItems` for a specific value of `_key`, or you can query across a specific home item (e.g. `/HomeItems/0`) for a specific name in its items. For more on this see https://stackoverflow.com/q/27207059 and https://stackoverflow.com/q/40656589 – Frank van Puffelen Sep 19 '21 at 16:28

0 Answers0