Raj kumar
Raj kumar

Reputation: 153

Android Firebase - retrieve particular value from Firebase realtime database

In my application I want to retrieve a single value from a particular id. The below screenshot is my database structure:

enter image description here

I want to retrieve the count of likes from database.

This is what I have tried:

 public String getLikesCount(String USER_ID){
    mref = FirebaseDatabase.getInstance().getReference("user").child(USER_ID);
    return currentLike;
}

I don't know what to do after this to get likes count. Can anyone help me please?

Upvotes: 2

Views: 2674

Answers (1)

Darush
Darush

Reputation: 12021

do the following:

mref = FirebaseDatabase.getInstance().getReference("user").child(USER_ID).child("likes");
mref.addListenerForSingleValueEvent(new ValueEventListener() {
 @Override
 public void onDataChange(DataSnapshot dataSnapshot) {
    String likes = dataSnapshot.getValue(String.class);
    //do what you want with the likes
 }

 @Override
 public void onCancelled(DatabaseError databaseError) {

 }
});

Upvotes: 1

Related Questions