【问题标题】:How to retrieve data from firebase database如何从firebase数据库中检索数据
【发布时间】:2017-05-29 08:45:45
【问题描述】:

我一直在使用 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) {

        }
    });

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

【问题讨论】:

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


【解决方案1】:

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

还可以使用List<Option> 而不是Options[] 作为选项。

【讨论】:

  • 只有当我删除属性选项和时间戳时才有效,我能做什么?
  • 尝试对时间戳使用长数据类型,对选项使用 List
  • 您的解决方案是什么?可能会帮助其他人:)
  • 我已将属性选项的类型改为List
猜你喜欢
  • 2016-11-27
  • 1970-01-01
  • 2018-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多