如何从firebase数据库中检索数据

时间:2017-05-29 08:45:45

标签: android firebase firebase-realtime-database real-time

我一直在使用Android和Firebase创建移动应用程序,我无法从firebase获取数据,问题出在行Survey survey = ds.getValue(Survey.class);,它只在我删除属性选项和时间戳时才有效,这是我的代码:

    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) {

        }
    });

enter image description here

    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 ;
}

1 个答案:

答案 0 :(得分:0)

根据https://firebase.google.com/docs/database/android/read-and-write,您需要一个默认构造函数来调用DataSnapshot.getValue(XY.class)

还可以使用List<Option>代替Options[]作为选项。