繁体   English   中英

如何从 Firebase 检索数据? dataSnapshot 有对象,但 getValue() 将返回 null

[英]How to retrieve data from Firebase? dataSnapshot has the object but getValue() will return null

我正在尝试制作一个计划扑克应用程序,但我无法使用数据库中的数据填充 recyclerview。 我的 Firebase 数据库如下所示:

数据库结构

错误代码:

public void getAllData(){
        databaseReference = FirebaseDatabase.getInstance().getReference();
        databaseReference.child("groups").addValueEventListener(new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot iter : dataSnapshot.getChildren()) {
                    Group group = iter.getValue(Group.class); 
                    for (Question question: group.getQuestionList()) {
                        RecyclerItem recyclerItem = new RecyclerItem(
                                question.getQuestionText(),
                                group.getStart_time(),
                                group.getEnd_time());
                        recyclerItemsList.add(recyclerItem);
                    }
                }

                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                throw databaseError.toException();
            }
        });
    }

Group group = iter.getValue(Group.class); 设置end_timestart_time ,但不能设置questions

这是我的 Group.java 文件:

package com.example.planningpokerproject.Model;

import android.os.Parcel;
import android.os.Parcelable;

import com.google.firebase.database.Exclude;
import com.google.firebase.database.PropertyName;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Group implements Parcelable {

    @PropertyName("start_time")
    private String start_time;

    @PropertyName("end_time")
    private String end_time;

    @PropertyName("questions")
    private ArrayList<Question> questions;


    public Group() {
    }

    public Group(String start_time, String end_time, ArrayList<Question> questionList){
        this.start_time = start_time;
        this.end_time = end_time;
        this.questions = questionList;
    }

    protected Group(Parcel in) {
        start_time = in.readString();
        end_time = in.readString();
        questions = in.createTypedArrayList(Question.CREATOR);
    }

    public static final Creator<Group> CREATOR = new Creator<Group>() {
        @Override
        public Group createFromParcel(Parcel in) {
            return new Group(in);
        }

        @Override
        public Group[] newArray(int size) {
            return new Group[size];
        }
    };

    public void setStart_time(String start_time) {

        this.start_time = start_time;
    }

    public void setEnd_time(String end_time) {
        this.end_time = end_time;
    }

    public void setQuestionList(ArrayList<Question> questionList) {
        this.questions = questionList;
    }

    public String getStart_time()
    {
        return this.start_time;
    }

    public String getEnd_time()
    {
        return this.end_time;
    }

    public ArrayList<Question> getQuestionList()
    {
        return this.questions;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Exclude
    public Map<String, Object> toMap() {
        HashMap<String, Object> result = new HashMap<>();
        result.put("start_time", start_time);
        result.put("end_time", end_time);
        result.put("questions", questions);

        return result;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(start_time);
        dest.writeString(end_time);
        dest.writeTypedList(questions);
    }
}

我的问题是如何使用iter.getValue(Group.class)检索questions ,或者我应该怎么做?

您可以创建对集合的引用并对其调用get()方法以创建对所有文档的查询。 然后,您可以简单地添加一个OnCompleteListener并访问通过task : Task<QuerySnapshot>检索到的文档task : Task<QuerySnapshot> ,如下所示。 您现在应该能够在每个文档快照上调用get()方法,以获取首选字段的值(在本例中为“问题”)。 代码在 kotlin 中,但如果您复制粘贴,Android Studio 应该能够将其转换为 java 文件。

val rootRef: FirebaseFirestore = FirebaseFirestore.getInstance()
val collRef: CollectionReference = rootRef.collection("groups")
var questions = arrayListOf<String>()
collRef.get().addOnCompleteListener {task: Task<QuerySnapshot> ->
       if(task.isSuccessful) {
             Log.d(TAG,"Success")
             for(document:DocumentSnapshot in task.result!!) {
                     questions = document.get("questions")) // you may need to add some code here to actually get the questions field's values as an array
             }
             adapter.notifyDataSetChanged() // notify the adapter about the new data
        }
}

请记住,如果您的数据库很大,您应该考虑使用数据分页来请求分段(页面)数据,而不是一次请求所有数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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