Pierre Pierre - 2 months ago 34
Android Question

Child List from Firebase with android

I'm trying to get the value of my firebase tree

enter image description here

I got a class get_categories

public class get_categories {
private String Name;

public get_categories(){

}

public String getName(){
return Name;
}
}


Then I declared the firebase

mRef = new Firebase("https://...../Users/Categories");


And try to retrieve my data with OnChildEventListener

mRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {

Log.e("Count " ,""+dataSnapshot.getChildrenCount());
//Log.e("VALUES " ,""+dataSnapshot.child("Details").getValue());
get_categories getCatName = dataSnapshot.child("Details").getValue(get_categories.class);
Log.e("NAME " ,""+getCatName.getName());
}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onCancelled(FirebaseError firebaseError) {

}
});


But I got an error


Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Name" (class com.app.pierre.myapp.get_categories), not marked as ignorable (0 known properties: ]) at [Source: java.io.StringReader@9d0f705; line: 1, column: 10] (through reference chain: com.app.pierre.myapp.get_categories["Name"])


What am I doing wrong ?

I used this case as reference:
https://www.firebase.com/docs/android/guide/retrieving-data.html

Answer

Well first change your Model like this,

public class GetCategories {
    private String Name;

    public GetCategories(){

    }

    public GetCategories(String name){
    this.Name = name;

    }

    public String getName(){
        return Name;
    }
}

and Push data using Model class Constructor..

GetCategories model = new GetCategories("yourValues");

mReference.child("Details").push().setValue(model);

and Now you can get it like this way..

mRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            for (DataSnapshot eventSnapshot : dataSnapshot.getChildren()) {

                GetCategories newPost = eventSnapshot.getValue(GetCategories.class);

                Log.e("NAME " ,""+ newPost.getName());   

            }

        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });