Reputation: 121
Is there any way to get another user's uid from realtime database? I want to get 2 name objects from the database with different uids at the same time.
Json file:
"Users" : {
"PnZdR1Ily5R5R58FYFVEuWN0Q855" : {
"-Kwwceeevvvvvttvv" : {
"name" : "abc"
},
"-Kccrr325gdfgdfFg" : {
"name" : "abc"
}
},
"MeTsW4SsW6e8hGrsFfVUuNNaQ3cj" : {
"-Kfvr345345354555" : {
"name" : "zxc"
},
"-Kbfhf56464646646" : {
"name" : "zxc"
}
}
}
This is my code:
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User details = dataSnapshot.getValue(User.class);
mName = details.name;
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "getUser:onCancelled", databaseError.toException());
}
};
mDatabase.child("Users").child($uid).addValueEventListener(postListener);
This is the part I'm stuck on, how do I get both uids at the same time?
Upvotes: 1
Views: 1882
Reputation: 18933
If you want to retrieve random key then you should use addChildEventValueListerner
because it gives u child of the Users node one by one.
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference mref = firebaseDatabase.getReference();
mref.child("Users").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.hasChildren()) {
User details = dataSnapshot.getValue(User.class);
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Upvotes: 2