简体   繁体   中英

How to retrieve data from firebase database

I have been creating mobile application using Android and Firebase, I can't get data from firebase, the problem is in the line Survey survey = ds.getValue(Survey.class); , it work only when I delete attributes options and timestamp, Here is my code:

    private FirebaseDatabase firebaseDatabase=FirebaseDatabase.getInstance();
private DatabaseReference mRootReference=firebaseDatabase.getReference();
private DatabaseReference mSurveysReference=mRootReference.child("surveys");


    mSurveysReference.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            for (DataSnapshot ds:dataSnapshot.getChildren()){
                Survey survey = ds.getValue(Survey.class);
                surveys.add(survey);
            }
        }

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

        }
    });

在此处输入图片说明

    public class Survey {
    public boolean acceptAnonymousVotes;
    public boolean enabled;
    public String id;
    public String image;
    public Option[] options  ;
    public  boolean resultPublic;
    public Timestamp timeStamp;
    public  String title;
    public int totalComments;
    public int totalViews;
    public int totalVotes;
    public  String type;
    public String userId;
public Survey() {
}
}

public class Option {
   public int id ;
   public String title ;
}

According to https://firebase.google.com/docs/database/android/read-and-write , you need a default constructor that is required for calls to DataSnapshot.getValue(XY.class)

Also use List<Option> instead of Options[] for options.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM