I am new to Firebase and Android. I have stored user authentication details (name, profile image other than email ID) in my Firebase account. I want to retrieve those data (such as names etc.,) to the other part of my app. How can I retrieve my realtime database child values?
Asked
Active
Viewed 2,699 times
0
-
http://stackoverflow.com/questions/27848313/firebase-retrieve-child-android?rq=1 – GoneUp Nov 23 '16 at 15:35
-
https://firebase.google.com/docs/database/android/read-and-write – JP Ventura Nov 23 '16 at 16:13
2 Answers
3
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("userID").child("displayName");
// Attach a listener to read the data at our posts reference
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String displayName = dataSnapshot.getValue().toString();
System.out.println(displayName );
}
For more: Read and Write Data on Android - Firebase Documentation

Nirel
- 1,855
- 1
- 15
- 26
-
dataSnapshot.getValue().toString(); goes to an error.called=method 'java.lang.String java.lang.Object.toString()' on a null object reference – Athul Antony NP Nov 24 '16 at 04:04
2
If you want to get the names of all the users, then you can do it like this
DatabaseReference mRef = FirebaseDatabase.getInstance().getReference();
mRef.child("users").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot: dataSnapshot.getChildren()){
Log.v("user_name", snapshot.getValue(String.class));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});

Hassnain Jamil
- 1,651
- 17
- 21