0

My app has two categories of users shop owner and buyers. When shop owner adds a shop I use his UID and then pushkey to add a shop like this:

this

In the Image "83yV" and "FmNu" are the shop owners and they add 2 and 1 shop respectively.

Now for buyers, I want to show the whole list of shops in a Recycler View.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_shops_avtivity);

    shopsDB = FirebaseDatabase.getInstance().getReference().child("shops");
}

@Override
protected void onStart() {
    super.onStart();
    FirebaseRecyclerAdapter<Shop,ShopViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Shop, ShopViewHolder>(
            Shop.class,
            R.layout.shop_single_layout,
            ShopViewHolder.class,
            shopsDB)
}

The problem is when I point my Firebase Recycler Adapter to shopDB, it returns two empty cardView like this:

this

When I point it to specific child like

shopsDB = FirebaseDatabase.getInstance().getReference().child("shops").child("83yV24a3AmP15XubhPGApvlU7GE2");

It returns shops add by "83yV".

How can I get the shops added by other owners also? Preferably without altering current DB or creating one DB with all shops in it within on child.

notakoba
  • 160
  • 1
  • 13
  • what do you get when u run this: groundDB = FirebaseDatabase.getInstance().getReference().child("grounds"); – Avinash Roy Jan 29 '18 at 11:32
  • @AvinashRoy That was my bad, Edited the question. When i run this : shopsDB = FirebaseDatabase.getInstance().getReference().child("shops");. I get two blank CardViews. – notakoba Jan 29 '18 at 11:42
  • you need to iterate through the shopsDB using a loop since it has some children – Avinash Roy Jan 29 '18 at 11:46
  • @AvinashRoy I tried that on datasnapshot like this : `public void onDataChange(DataSnapshot dataSnapshot) { for (DataSnapshot child: dataSnapshot.getChildren()) { Log.d("User key", child.getKey());` and it returns each key in shops but I can't figure out how to put these keys into shopsDB path automatically. Even if i do it somehow it will create another problem that is once the shopsDB path goes to a new child, results of an old child will be gone. – notakoba Jan 29 '18 at 11:56
  • Where is `shops` node in your database? Please a more detailed structure. – Alex Mamo Jan 29 '18 at 12:04
  • @AlexMamo shops node is just above UID "83yV". It is the parent of all the show-owners UID. DB is like this `shops` -> `shop owners UID` ->`shops` – notakoba Jan 29 '18 at 12:10

1 Answers1

1

To achieve this, you need to use getChildren() method twice, once to get all the users and second to get all the shops.

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference shopsRef = rootRef.child("shops");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            for(DataSnapshot dSnapshot : ds.getChildren()) {
                String name = dSnapshot.child("name").getValue(String.class);
                Log.d("TAG", name);
            }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
shopsRef.addListenerForSingleValueEvent(eventListener);

Using this code, you'll be able to display all shop names to the buyers.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • It is returning all shop names as you said but how can I use it to populateViewHolder and show it in recycler view? – notakoba Jan 29 '18 at 13:03
  • Just create a `Query` and pass it to your `FirebaseRecyclerAdapter`. That's it! – Alex Mamo Jan 29 '18 at 13:36
  • Just one more question. My app is going to have maximum 1000 shops so would it be more economical to make an `allShops` node and put all shops in it directly. I know it will lead to duplication of data in `shops` and `allShops` node but then I can point `FirebaseRecyclerAdapter` directly to `allShops` node and limit it to some value rather than doing `for(DataSnapshot ds : dataSnapshot.getChildren()) { for(DataSnapshot dSnapshot : ds.getChildren()) {` `**On create**` and then querying it in `onStart` ? – notakoba Jan 29 '18 at 13:51
  • Accepted your answer,Thank you, so much for all the help. – notakoba Jan 29 '18 at 14:14