我一直在阅读Firebase文档,但它只显示了简单的示例。我需要做的是从我的数据库的根目录中获取所有数据,并将其存储到一个自定义Java对象的数组中(现在,我只是想将一个调查集合到一个对象中)。根的每个子节点都是一个Survey,我有一个部分设置的类来接收数据。
我知道我使用这段代码:
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
Survey s = childSnapshot.getValue(Survey.class);
s.toString();
}
}
这是我的Survey.java类。我不确定如何在Firebase中镜像Survey对象的结构:
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
/* This class stores one survey from the database */
public class Survey implements Serializable {
String name;
String company;
String description;
String num_questions;
String user;
//HashMap<String,ArrayList<String>> question_strings; // Stores question title as key, with an arraylist of answer choices as the value
public Survey() { }
public Survey(String name, String company, String description, HashMap<String,ArrayList<String>> question_strings)
{
this.name = name;
this.company = company;
this.description = description;
//this.question_strings = question_strings;
}
public void setName(String name) { this.name = name; }
public void setCompany(String company) { this.company = company;}
public void setDescription(String description) { this.description = description;}
public void setNumQuestions(String num) { this.num_questions = num;}
public void setUser(String user) { this.user = user;}
public String getName() { return this.name;}
public String getCompany() { return this.company;}
public String getDescription() { return this.description;}
public String getNumQuestions() { return this.num_questions;}
public String getUser() { return this.user;}
public String toString()
{
System.out.println("Name: " + name);
System.out.println("Company: " + company);
System.out.println("Description: " + description);
System.out.println("Num questions: " + num_questions);
return "";
}
}
我应该如何构建我的Survey类以使用Firebase?
question_answers对我来说就像是一个List<List<String>>
。你试过吗?
从我看到的:
公司是一个字符串
描述是一个字符串
名称是一个字符串
num_questions是一个Int
用户是一个字符串
SurveryId很长
question_answers有点复杂。但它似乎是一个字符串列表的列表。所以List>。
P.S num_questions似乎是多余的,因为您已经可以通过获取question_answers列表的大小来了解问题的数量。