I have a database structure for chatting application so i have a message node in database looks like this
messages is parent of a key the contains the UIDs of the two users chatting and this key contains the value of the chat body, I the application is working so far so good when pushing data done as i want.
But when trying to read the data the DataSnapShot object always have the the last leaf of the node means the child of the key. I tried different approaches to get the key it self to iterate through and get a list of its values with no luck.
Approach i tried
public static final String MESSAGES_NODE_DB = "messages";
mMessageDbRef = FirebaseDatabase.getInstance().getReference()
.child(MESSAGES_NODE_DB).child(keyUserA + "-" + KeyUserB);
mMessageDbRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Timber.d("parent key " + dataSnapshot.getKey());
for (DataSnapshot singleSnap : dataSnapshot.getChildren()) {
Message message = singleSnap.getValue(Message.class);
messageList.add(message);
instantiateRecyclerView();
}
}
This approach always cause an error Can't convert object of type
java.lang.String
And parent key prints L91eMRVq_nx6WHUzZFo
Another one
mMessageDbRef = FirebaseDatabase.getInstance().getReference().child("messages");
for (DataSnapshot singleSnap : dataSnapshot.getChildren()) {
Timber.d("keys" + singleSnap.getKey());
}
}
The output is L91eMRVq_nx6WHUzZFo
What i want to achieve
To retrieve the list of keys inside of messages node as
vXgRtbjqhUYPOLyjhmGKRzITUC83-24L0kQx75fhoz06YpXnWRheETct2
Inside onChildAdded method do not use getChildren() and the for loop method as the onChildAdded method returns the individual nodes below the mMessageDbRef reference. Basically the data snapshot parameter in onChildAdded can be converted to a Message object directly.
Thus, directly assign the data snapshot value to the message object as you have done without using any loop or the getChildren method.
And by doing this you will have access to the value of the message through the message object variables.
Something like this:
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Message message = dataSnapshot.getValue(Message.class);
messageList.add(message);
instantiateRecyclerView();
}
}
To retrieve the key inside the messages
node:
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("messages").child(keyUserA + "-" + KeyUserB);
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
String keys=datas.getKey();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
the datasnapshot is on child(keyUserA + "-" + KeyUserB);
then you iterate inside the randomid L91eMRVq_nx6WHUzZFo
and you will be able to retrieve it using getKey()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.