Java – Retrieves data from Firebase Android

Retrieves data from Firebase Android… here is a solution to the problem.

Retrieves data from Firebase Android

I studied in France but couldn’t find a French tutorial to solve my problem. Therefore, I have to raise my problem in the hope that there is a satisfactory solution.

My problem is that I’m having trouble reading data on Firebase, which I’ve spent three days.

I have a structure like this:

I’ve started writing some code where I was able to recover keys but I couldn’t recover those values “nom”, “argent”, etc.

private Firebase mRef ;
mRef = new Firebase("https://authent-50e6b.firebaseio.com/users");
mRef.addChildEventListener(new ChildEventListener() { 
    @Override
   public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        String cle = dataSnapshot.getKey();
        Toast.makeText(ListeActivity.this,"la cle est : "+cle ,Toast.LENGTH_SHORT).show();

Solution

John’s alternative is to use DataSnapshot.child() to get each property:

public void onChildAdded(DataSnapshot dataSnapshot, String s) {
    String cle = dataSnapshot.getKey();
    String nom = dataSnapshot.child("nom").getValue(String.class);
    Toast.makeText(ListeActivity.this,"la cle est : "+cle+" nom est : "+nom ,Toast.LENGTH_SHORT).show();

Related Problems and Solutions