Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

retrieving data from firebase android

I am studying in France and I have trouble finding French tutorials to solve my problem. I am therefore obliged to ask my question, hoping to have a satisfactory solution.

My problem is that I have trouble reading my data on firebase and I have spent three days on it.

I have a structure like this:

I had started to code something, I was able to recover the key but I could not recuperate these 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();
like image 528
Charaf Ahmed Avatar asked Mar 18 '26 16:03

Charaf Ahmed


2 Answers

An alternative to John's is to use DataSnapshot.child() to get to 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();
like image 153
Frank van Puffelen Avatar answered Mar 20 '26 05:03

Frank van Puffelen


You would need something like following (assuming you have User pojo whose fields map to those in firebase db)

User user = dataSnapshot.getValue(User.class);
String nom = user.getNom();
like image 32
John O'Reilly Avatar answered Mar 20 '26 05:03

John O'Reilly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!