0

In the code sample the key nodes below Bookings are userIds(2 users) followed by a random push() key. I want to retrieve the data nested within both the userIds.

data

databaseReference = FirebaseDatabase.getInstance()
    .getReference()
    .child("Bookings")
    .child(user.getUid());

The reference above only applies to the current logged in user

François Maturel
  • 5,884
  • 6
  • 45
  • 50
Marcos Maliki
  • 510
  • 5
  • 16
  • Refer my answer here may be helpful to you :https://stackoverflow.com/questions/44427213/android-get-value-from-firebase-database/44427384#44427384 – ashish Jun 14 '17 at 13:48

1 Answers1

0

Please use this code:

databaseReference = FirebaseDatabase.getInstance().getReference().child("Bookings");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String userId = dataSnapshot.getKey();

        DatabaseReference keyRef = FirebaseDatabase.getInstance().getReference().child("Bookings").child(userId);
        ValueEventListener valueEventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot ds : dataSnapshot.getChildren()) {
                    String Client_name = ds.child("Client_name").getValue(String.class);
                    String Data = ds.child("Data").getValue(String.class);
                    //and so on
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {}
        };
        keyRef.addListenerForSingleValueEvent(valueEventListener);
    }

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

Hope it helps.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • The sample above should work fine (userId without quotes) but my problem is just the reference. I'm retrieving the data via a getter class into a recycler view. – Marcos Maliki Jun 16 '17 at 03:32
  • I have just removed the quotes. Thanks for your observation. Because i didn't see your class model or some code, i have recomended you solving this problem in this way, which is also the easiers way to do it. Hope you figured it out and keep me posted. – Alex Mamo Jun 16 '17 at 09:07