我试图从Objects
collection
中获取Firestore
的列表,如下所示:
Query query = FirebaseFirestore
.getInstance()
.collection("objects")
.limit(10);
query.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot snapshot,
@Nullable FirebaseFirestoreException e) {
// Handle errors..
ObjectList = snapshot.toObjects(ObjectModel.class);
// Update UI..
}
});
我的对象类:
public class ObjectModel {
// Some other fields..
private Double mPosition;
public ObjectModel() { }
public Double getPosition() { return mPosition; }
public void setPosition(Double position) { mPosition = position; }
}
但后来我得到了:
Could not deserialize object. Failed to convert a value of type java.lang.String to double (found in field 'position')
position
字段属于Number
类型,因此,为什么我会收到String
?
修改
文件样本:
答案 0 :(得分:0)
在你的班级中添加:
//--PROPERTIES--
@SerializedName("field1")
@Expose
private String field1;
@SerializedName("field2")
@Expose
private String field2;
//-----METHOD------
@Override
public String toString() {
return fields1;
}
@Override
public String toString() {
return fields2;
}
答案 1 :(得分:0)
正如道格·史蒂文森(Doug Stevenson)所指出的那样,问题在于我不小心将其降级到比最新的15.0.0
更低的版本。