0

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?

enter image description here

adjuremods
  • 2,938
  • 2
  • 12
  • 17
Athul Antony NP
  • 43
  • 2
  • 12

2 Answers2

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