Reputation: 1304
I've made an chat app just for me and my friend.
When I message them, I want to store their Uid
.
How do I do that ? Also mention if its needed to reconstruct the entire structure.
I have thought of String[] uidList = DatabaseReference.child("Users").getKey();
The Messages are not created yet. I want to create them with friend_user_id which I can't retrieve at this moment.
Below is how I want My Firebase Database structure to look like this :
app-id:
Users:
my_user_id:
name:"Me"
last_seen:"MyLastSeenTimeValue"
friend_user_id:
name:"Friend"
last_seen:"FriendLastSeenTimeValue"
Messages:
my_user_id:
friend_user_id:(How to retrieve this ?)
random_msg_id_1:
text:"Hi"
msg_type:"text"
time:"msgTime"
random_msg_id_2:
text:"Hello"
msg_type:"text"
time:"msgTime"
friend_user_id:(How to retrieve this ?)
my_user_id:
random_msg_id_1:
text:"Hi"
msg_type:"text"
time:"msgTime"
random_msg_id_2:
text:"Hello"
msg_type:"text"
time:"msgTime"
Upvotes: 1
Views: 1501
Reputation: 16976
Not sure about the Json
output but as a guess, it should be something like following codes:
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference();
myRef.child("Messages").child("my_user_id").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot item_snapshot:dataSnapshot.getChildren()) {
// get the data here and loop through the friend_user_id to get the nodes like following
Log.d("User id ", item_snapshot.toString());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
// handle error
}
});
Upvotes: 1