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