user7857570
user7857570

Reputation:

How to get the list of child nodes from firebase database reference?

DB Structure

Below I've attached the picture of DB Structure and I'm trying to get the list of child nodes from firebase DB reference (timingInformation). How can I get the list?

Upvotes: 1

Views: 1369

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599541

To read/synchronize data from the Firebase Database, you'll need to attach a listener. A simple case:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference("timingInformation");
ref.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
        Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());
    }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
        Log.d(TAG, "onChildChanged:" + dataSnapshot.getKey());
    }

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {
        Log.d(TAG, "onChildRemoved:" + dataSnapshot.getKey());
    }

    @Override
    public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
        Log.d(TAG, "onChildMoved:" + dataSnapshot.getKey());
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.w(TAG, "onCancelled", databaseError.toException());
    }
};

For more see the Firebase documentation on loading data from a list, and the reference documentation on what you can do with a snapshot.

Upvotes: 4

Related Questions