Firebase数据库快照以动态回收视图数组列表



我的数组:

private static String[] date = {
"Mar 15",
"Mar 22",
"Mar 23" }; 
private static String[] type_d = {
"Day",
"Night",
"Morning" };

用数组填充recycleview:

public static ArrayList getList() {
ArrayList<TestModel> list = new ArrayList<>();
for (int i = 0; i < date.length; i++) {
testModel = new TestModel();
testModel.setDate(date[i]);
testModel.setType(type_d[i]);
list.add(testModel);
}
return list;
}

这很完美。现在我正试图从firebase实时数据库加载精确的数据(类似json格式(

if(dataSnapshot.exists() && dataSnapshot.getChildrenCount()>0) {
Map<String, Object> map = (Map<String, Object>) dataSnapshot.getValue();
if (map.get("type_d") != null) {
type_d = map.get("type_d").toString(); //returns type normally
}
if (map.get("date") != null) {
date = map.get("date").toString(); //returns date normally
}
}

我的火球数据

{
"test": [
{
"type_d": "Day",
"date": "20 Mar"
},
{
"type_d": "Night",
"date": "11 Mar"
}
]
}

Q: 我将如何将这些数据放入testModel

你可以这样做:

for (postSnapshot in dataSnapshot.children) {
val post = postSnapshot.getValue<TestModel>()
testModelList.add(post)
}

最新更新