无法将java.lang.Long类型的对象转换为java.util.Date类型

Isa*_*lle 2 android firebase firebase-realtime-database

我正在迁移到Firebase新版本,并且在尝试从快照获取值时出现以下错误

com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.Long to type java.util.Date
Run Code Online (Sandbox Code Playgroud)

尝试通过以下方法从快照获取值时会发生这种情况

public void getUpdates(DataSnapshot dataSnapshot){
    Item item = dataSnapshot.getValue(Item.class);
    itemArrayList.add(item);
    itemAdapter.refreshItem(itemArrayList);
}
Run Code Online (Sandbox Code Playgroud)

我想它与Item对象有关,但它之前有用,所以我无法弄清楚出了什么问题.我确实在使用日期.

Firebase项目结构

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;
Run Code Online (Sandbox Code Playgroud)

任何帮助将受到高度赞赏.

Shu*_*ank 5

Firebase不支持Date类对象,因此您需要将它们存储为已经完成的long/timestamp.

1463845578489 是一个long,需要存储在long变量而不是Date中

将变量声明更改为

private long publishedDate;
Run Code Online (Sandbox Code Playgroud)

然后,要将long转换为有效的Date对象,您可以使用它

Date d = new Date(publishedDate);
Run Code Online (Sandbox Code Playgroud)