I am trying to fetch the user details of a particular user using his UID. I need a method that can return the userName as a String type so that I can store it and display it in my app.
getUserName("UID");
This is how my database looks like -
This is what i tried doing -
private String getUserName(String startedBy) {
mProfileRef=database.getReference("Profile").child(startedBy).child("UserDetails").child("userName");
mProfileRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
userName = dataSnapshot.getValue(String.class);
Log.i(TAG, "onDataChange: "+userName);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return userName;
}
The Log statement executes perfectly and gives the correct userName based on the UID. The method does not return the userName. It returns null. How do I return the userName?