我正在为我的Android应用使用Firebase数据库。 我有实体:
public class Goal implements Serializable {
private double averageSpeed;
private long duration;
private Date startDate;
private Date endDate;
private LinkedList <LatLng> locations;
...
}
我正试图从数据库中获取这些对象:
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Goal goal = dataSnapshot.getValue(Goal.class);
goalAdapter.add(goal);
}
但后来我收到了错误
类java.util.List有泛型类型参数,请使用GenericTypeIndicator代替异常
我的数据库的JSON结构:
{
"goals" : {
"-KdPyQTvht4seniKUk6o" : {
"averageSpeed" : 0,
"distance" : 0,
"duration" : 619,
"endDate" : {
"date" : 20,
"day" : 1,
"hours" : 12,
"minutes" : 3,
"month" : 1,
"seconds" : 21,
"time" : 1487585001328,
"timezoneOffset" : -120,
"year" : 117
},
"locations" : [ {
"latitude" : 50.5066025,
"longitude" : 30.4184
}, {
"latitude" : 50.506281,
"longitude" : 30.4189611
} ],
"startDate" : {
"date" : 20,
"day" : 1,
"hours" : 12,
"minutes" : 2,
"month" : 1,
"seconds" : 44,
"time" : 1487584964181,
"timezoneOffset" : -120,
"year" : 117
}
}
}
}
如何更改代码以使一切正常?
答案 0 :(得分:0)
你让它变得比必要的更困难。只有当您在一次调用中从DataSnapshot检索目标列表时,才需要GenericTypeIndicator
。如果您只需要一个目标,则可以将课程传递给getValue()
:
Goal goal = dataSnapshot.getValue(Goal.class);
当您关注它时,您可能希望将List
更改为类型安全的集合:
private List<Goal> locations;