无法将类型 java.lang.Long 的对象转换为类型 java.util.Date
Can't convert object of type java.lang.Long to type java.util.Date
我正在迁移到 Firebase 新版本,尝试从快照获取值时出现以下错误
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.Long to type java.util.Date
尝试使用以下方法从快照中获取值时会发生这种情况
public void getUpdates(DataSnapshot dataSnapshot){
Item item = dataSnapshot.getValue(Item.class);
itemArrayList.add(item);
itemAdapter.refreshItem(itemArrayList);
}
我猜是跟Item对象有关,不过之前是有效果的,不知道是什么问题。我确实在使用日期。
Firebase 项目结构
项目对象
private String title;
private String description;
private HashMap<String, ItemPicture> picturesHashMap;
private Date publishedDate;
private Date deletionDate;
private String condition;
private String delivery;
private String uid;
private int reported;
private boolean given;
private Location location;
private String frontImage;
private String uniqueID;
非常感谢任何帮助。
Firebase 不支持 Date class 对象,因此您需要将它们存储为您已经完成的 long/timestamp。
1463845578489
是一个 long,需要存储在一个 long 变量中,而不是 Date
将您的变量声明更改为
private long publishedDate;
然后要将 long 转换为有效的 Date 对象,您可以使用此
Date d = new Date(publishedDate);
我正在迁移到 Firebase 新版本,尝试从快照获取值时出现以下错误
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.Long to type java.util.Date
尝试使用以下方法从快照中获取值时会发生这种情况
public void getUpdates(DataSnapshot dataSnapshot){
Item item = dataSnapshot.getValue(Item.class);
itemArrayList.add(item);
itemAdapter.refreshItem(itemArrayList);
}
我猜是跟Item对象有关,不过之前是有效果的,不知道是什么问题。我确实在使用日期。
Firebase 项目结构
项目对象
private String title;
private String description;
private HashMap<String, ItemPicture> picturesHashMap;
private Date publishedDate;
private Date deletionDate;
private String condition;
private String delivery;
private String uid;
private int reported;
private boolean given;
private Location location;
private String frontImage;
private String uniqueID;
非常感谢任何帮助。
Firebase 不支持 Date class 对象,因此您需要将它们存储为您已经完成的 long/timestamp。
1463845578489
是一个 long,需要存储在一个 long 变量中,而不是 Date
将您的变量声明更改为
private long publishedDate;
然后要将 long 转换为有效的 Date 对象,您可以使用此
Date d = new Date(publishedDate);