Reputation: 63
I have my database tree structure like this:
Please tell me how do I get the value of "username" if I have the value of "userid"
Upvotes: 0
Views: 56
Reputation: 138804
how do I get the value of "username" if I have the value of "userid"
To solve this, please use the following lines of code:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("users").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String username = dataSnapshot.child("username").getValue(String.class);
Log.d(TAG, username);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
The output in the logcat will be:
hisoka
Upvotes: 1