This is my firebase chat structure(see image below). I want to get only amitpal_name value not the others but this method fetching me all the values
I had also use this
here is your reference
refAmitPat = FirebaseDatabase.getInstance().getReference().child("ChatMessage").child("msg");
and here is child event listner on your DB Reference
refAmitPat.child("amitpal_name").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
Log.d("amitPal",dataSnapshot.getValue()+"");
// add message value to list and call notify datasetchange method
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
Log.d("amitPal",dataSnapshot.getValue()+"");
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
what are you doing, wrong is, you are iterating the datasnapshot whereas when you push the message under usernames as child, the child will get as the datasnapshot in childAdded method, means the data snapshot your are iterating, is actually your message with push-key and message value. so there's no need to iterate through that. simply call datasnapshot.getValue() function to get message value