Juliana Lam
Juliana Lam

Reputation: 3

How do I retrieve a specific information on firebase database?

I am new to android programming and wish to explore more on Android programming. My current database tree looks like this database tree strucutre and this is how I push the data into the database push info into database.

Can any professional teach me on how to retrieve a specific data (e.g. name / phone number) from a specific user in the user list and display them?

Thanks in advance!

Update: After trying several solutions, the only code that can retrieve the name is this

databaseReference = FirebaseDatabase.getInstance().getReference().child("Users").child("-MA5f3qb0nBBTkViv-pz");

I even tried to assign the UID to a string and put in into the second child like this String UID = firebaseAuth.getCurrentUser().getUid();

But the code will not run too with the error of "Attempt to invoke virtual method on a null object reference".

Upvotes: 0

Views: 54

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80944

To retrieve information try the following:

DatabaseReference reference = FirebaseDatabase.getInstance().getReference("User");

reference.addListenerForSingleValueEvent(new ValueEventListener() {
 @Override
public void onDataChange(DataSnapshot dataSnapshot) {
   User userInfo = dataSnapshot.getValue(User.class);

 }
   @Override
public void onCancelled(DatabaseError databaseError) {
    throw databaseError.toException();
  }
 });

Add a reference to node User and then using addListenerForSingleValueEvent you can retrieve the values.

Upvotes: 2

Related Questions