I am trying to query a child node value but I don't know parent key, I want to query for Firebase Adapter. I want to access userid node under area but I don't know the Id of parent i.e -KqPJMsjSb5CbPcq4nXv
Here is the snapshot of Record:
Asked
Active
Viewed 1,462 times
0

Jawad Hassan Soomro
- 512
- 5
- 24
-
1You seem to have two dynamic/push keys under `/areas`. Firebase Realtime Database queries work on a flat list of nodes, and order/filter on a value at a fixed location under each direct child. See https://stackoverflow.com/questions/27207059/firebase-query-double-nested. – Frank van Puffelen Oct 06 '21 at 03:30
1 Answers
2
Please use this code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userIdRef = rootRef.child("areas").child(userId);
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String areaId = ds.child("areaId").getValue(String.class);
Boolean booked = ds.child("booked").getValue(Boolean.class);
Integer bookingHour = ds.child("bookingHour").getValue(Integer.class);
//and so on
Log.d("TAG", areaId + " / " + booked + " / " + bookingHour);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
userIdRef.addListenerForSingleValueEvent(eventListener);
In which userId
is the id of the user that said that is not missing.
When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.

Alex Mamo
- 130,605
- 17
- 163
- 193
-
The solution mentioned by you @AlexMamo doesnot work, don't forget that there is a key node after areas – Jawad Hassan Soomro Aug 01 '17 at 12:06
-
Why are you saing that, have you test it? I can say that it will work for sure. As you probably see, the DatabaseReference userIdRef is `rootRef.child("areas").child(userId)`, this means that we got down in the tree right to the `userId` node. Using `getChildren()` means that we are getting all the childrens from there. Because in this care is only one child, we can get all the childrens from that node, `areaId, booked, bookingHour and so on`. – Alex Mamo Aug 01 '17 at 12:15