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
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("ChatMessage" ).child( "msg").child("amitpal_name");
ref.addValueEventListener(new ValueEventListener() {
@SuppressLint("SetTextI18n")
@Override
public void onDataChange ( @NonNull final DataSnapshot dataSnapshot2){
}
}
check the answer for this post this may help: I want to list all unique childrens and keys along with values in a spinner firebase
In this, you can see how to fetch each of the node values then key along with their values.
mUserDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childDataSnapshot : dataSnapshot.getChildren()) {
//"It will give you all node values:"+dataSnapshot.getKey()
//"You can check ":
if(dataSnapshot.getKey()=="amitpal"){
for (DataSnapshot childDataSnapshot2 : childDataSnapshot.getChildren()) {
// "CHILD KEY"+childDataSnapshot2.getKey()
arrayList.add(childDataSnapshot2.getKey());
//"CHILD VALUES"+childDataSnapshot2.getValue()
}}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
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
change your below line
String chatis=(String) ((DataSnapshot)iterator.next()).getValue();
to
String chatis=((DataSnapshot)iterator.next()).getValue().toString();
Updated method below
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
Log.e( "OUTER ADDED IS-" , dataSnapshot.getValue().toString());
Iterator iterator=dataSnapshot.getChildren().iterator();
while (iterator.hasNext()){
DataSnapshot local = (DataSnapshot) iterator.next();
String chatis=local.getValue().toString();
Log.e( "Inner ADDED IS-" , chatis);
}
}
EDIT
also below line as well after the reference initialization
dbreference.keepSynced(true);