【发布时间】:2019-11-23 22:46:37
【问题描述】:
我正在尝试制作一个计划扑克应用程序,但我无法使用数据库中的数据填充 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_time 和start_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 或者我应该怎么做?
【问题讨论】:
-
您想从数据库中的每个文档中检索问题数组,还是在用数据填充回收站视图时遇到问题?
-
我想检索问题。对不起,如果我不够清楚。
-
只是标题有误导性,请尽量改。就检索而言,您应该能够调用
iter.get("questions)并传入字段名称,这里是问题。这应该会返回您可以使用的该字段中的数据,但是您认为合适 -
我试过了,但
iter没有get 功能。iter.getValue().get("questions")在手表中有效,但在代码中无效。它无法编译,因为iter和iter.getValue()都没有 get 方法。
标签: java android firebase firebase-realtime-database android-recyclerview