无法使用Firebase实例化类型的值

时间:2015-12-17 13:16:22

标签: android firebase firebase-realtime-database

{  
"patients": {
"-Kx5vuRqItEF-7kAgVWy": {
    "name": "name1",
    "age": "27",
    "graphData": {
        "-K5knK9j8EfUHS7AesFw": {
            "date": "16/12/2015",
            "graphList": "[...]"
        }
    }
},
"-Kx5vuRqItEF-7kAgVWy": {
    "name": "name1",
    "age": "27",
    "graphData": {
        "-K5knK9j8EfUHS7AesFw": {
            "date": "16/12/2015",
            "graphList": "[...]"
        },
        "--K5knNeBFoLrjWnZshfL": {
            "date": "12/12/2015",
            "graphList": "[...]"
        },

    }
  }
 }
}

PatientView是Java对象,它包含另一个嵌套的Graph Object

public class PatientView {
private String name;
private String age;
private boolean gender;
private float height;
private String phone;
private Object bdate;

private String image;

private ArrayList<Graph> graphData;

public PatientView() {
}
}

图形对象:具有日期和ArrayList对象

public class Graph implements Serializable {
private Object graphDate;
private ArrayList<Double> graphList;

// Empty constructor needed for firebase
public Graph() {

}

public Graph(Object graphDate, ArrayList<Double> graphList) {
    this.graphDate = graphDate;
    this.graphList = graphList;
}
}

并通过firebase addChildEventListen

读取值
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            if (dataSnapshot != null && dataSnapshot.getValue() != null) {

                PatientView p = dataSnapshot.getValue(PatientView.class);

                patients.add(p);
                patientIdsFromServer.add(dataSnapshot.getKey());
                recyclerView.scrollToPosition(patients.size() - 1);
                mAdapter.notifyItemInserted(patients.size() - 1);
            }
        }

但是收到错误

Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at [Source: java.io.StringReader@2ced7c6a; line: 1, column: 62] (through reference chain: com.iitb.openmrs.openmrsfinal.ui.Adapter.PatientView["graphData"])

问题是,当我将数据推送到graphData子系统时,它会生成id,因此架构与Graph Object不匹配。我搜索是否有任何方法可以在读取数据时没有找到任何内容来抑制id。

1 个答案:

答案 0 :(得分:0)

这里的问题是graphData中的数据不是数组。 Firebase没有对阵列的本机支持。如果你存储一个数组,它实际上存储为一个“对象”,整数作为键名,因为你的键不是整数,你不能将它用作数组。

您应该查看this SO answer.