ALSP
ALSP

Reputation: 102

How to retrieve child data under 2 random keys

How do I display the RequestsID on a recyclerview only when stallID = 1? How should my DatabaseReference be?

enter image description here

update:

enter image description here

enter image description here

Upvotes: 0

Views: 151

Answers (1)

PradyumanDixit
PradyumanDixit

Reputation: 2375

Your database structure would not support orderByChild() from top node. So you would have to set your database reference upto the node food and then you can use orderByChild() query to find the Childs with certain stallID.

This, in code would look something like this:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Requests").child("1542..").child("foods");

  ref.orderByChild("stallID").equalTo(stallIDYouWant).addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                            for (DataSnapshot ds : dataSnapshot.getChildren()){
                                 // do here what you wanted
                            }

                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) { 
                              // ToDo: don't ignore this, do something for errors

                        }
                  )};               

This would in fact not help you much because you'd have to set this for every unique id in your database structure.

So the better thing to do here would be make your database structure different. To know more how to make your structure good for this operation, refer this answer.

Upvotes: 1

Related Questions