Reputation: 129
I'm trying to make a chat application using firebase database but I'm having some problems.
This is my database. I want to add a listener to Messages but just for the values for User->UID->chats->Chat IDs
. How can I do this?
Upvotes: 0
Views: 400
Reputation: 2165
You need to do it twice: first get the chat id list based on user id, then get the messages data for each of those chat ids.
// consider you have you user (FirebaseUser) object ready
FirebaseDatabase.getInstance().getReference("Users/" + user.uid + "/chats")
.addListenerForSingleValueEvent(new ValueEventListener() {
... onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
FirebaseDatabase.getInstance().getReference("Messages/"+snapshot.getKey())
.addListenerForSingleValueEvent(...
... onDataChange(DataSnapshot messageSnapshot) {
// I changed the snapshot name to make it easier to read
// here messageSnapshot.getKey() is message ID
// and messageSnapshot.getValue() will contain data you need (I think)
}
...
);
}
}
...
});
Hope this helps
Upvotes: 1
Reputation: 6282
Here is an example to access
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference reference = database.getReference("Messages").child(read);
Upvotes: 0