简体   繁体   English

Firebase 数据库快照以动态回收查看数组列表

[英]Firebase database snapshot to recycleview arraylist dynamically

My array:我的阵列:

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

Populate recycleview with array:用数组填充回收视图:

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;
}

This works perfect.这工作完美。 Now i'm trying to load exact data (in json like format) from firebase realtime database现在我正在尝试从 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
            }
        }

my firebase data我的 Firebase 数据

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

Q: how im gonna put this data to testModel ?问:我如何将这些数据放入testModel

You can do it like this:你可以这样做:

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

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

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