簡體   English   中英

如何從Firebase數據庫檢索數據

[英]How to retrieve data from firebase database

我一直在使用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 ;
}

根據https://firebase.google.com/docs/database/android/read-and-write ,您需要一個默認構造函數,該構造函數是調用DataSnapshot.getValue(XY.class)所必需的

也可以使用List<Option>代替Options[]作為選項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM