user14141430
user14141430

Reputation:

How to read data from the Firebase realtime database

I want to read data from a particular node from the firebase realtime database. This is my JSON tree:

{
  "user" : "1",
  "user_details" : {
    "emails" : [ "[email protected]" ],
    "usernames" : [ "valk stone" ]
  }
}

How do I read data from the user node?

Upvotes: 1

Views: 152

Answers (1)

Ashish
Ashish

Reputation: 6919

You can read data with the help of listener. If you read docs you come to know that SingleValueEventis used for read data once :

ValueEventListener userListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Log.d(dataSnapshot);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        // Getting Post failed, log a message
        Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
        // ...
    }
};
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("user_details");
usersRef.addListenerForSingleValueEvent(userListener);

Upvotes: 1

Related Questions