Athul Antony NP
Athul Antony NP

Reputation: 43

Firebase retrieving the child value from realtime database of Firebase

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

Upvotes: 0

Views: 2716

Answers (2)

Hassnain Jamil
Hassnain Jamil

Reputation: 1681

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) {

            }
        });

Upvotes: 2

Nirel
Nirel

Reputation: 1925

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

Upvotes: 3

Related Questions