0
votes

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: The area node have different Areas under a auto generated Id's

1
You 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 stackoverflow.com/questions/27207059/….Frank van Puffelen

1 Answers

2
votes

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.