无法将java.lang.Long类型的对象转换为java.util.Date类型 [英] Can't convert object of type java.lang.Long to type java.util.Date
问题描述
我正在迁移到Firebase新版本,尝试从快照中获取 getvalue 时,出现以下错误:
com.google.firebase.database.DatabaseException:无法将java.lang.Long类型的对象转换为类型java.util.Date
$ p $这样的情况发生在试图通过下面的方法从快照中获取价值的时候发生的。
public void getUpdates(DataSnapshot dataSnapshot){
Item item = dataSnapshot.getValue(Item.class);
itemArrayList.add(item);
itemAdapter.refreshItem(itemArrayList);
}
我猜这跟Item对象有关系, ,所以我无法弄清楚什么是错的。我确实使用了日期。
Firebase项目结构
项目对象
private字符串标题;
私有字符串描述;
私有HashMap< String,ItemPicture> picturesHashMap;
私人日期publishedDate;
私人日期deletionDate;
private String条件;
私人字符串传送;
private String uid;
私有int报告;
私人布尔给定;
私人位置位置;
私人字符串frontImage;
private String uniqueID;
任何帮助都将得到高度评价。
解决方案Firebase不支持 Date 类对象,因此您需要将它们存储为您已经完成的long / timestamp。
1463845578489
是一个长整型,它需要存储在一个long变量中,而不是一个Date
<将您的变量声明更改为
private long publishedDate;
然后,将long转换为有效的Date对象,您可以使用这个
Date d = new Date(publishedDate);
I'm migrating to Firebase new version and I get the following error when trying to getvalue from the snapshot
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.Long to type java.util.Date
This happens when trying to getvalue from the snapshot in the following method
public void getUpdates(DataSnapshot dataSnapshot){ Item item = dataSnapshot.getValue(Item.class); itemArrayList.add(item); itemAdapter.refreshItem(itemArrayList); }
I guess it has something to do with the Item object, but it worked before, so I can't figure out what's wrong. I'm using dates indeed.
Firebase item structure
Item object
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;
Any help would be highly appreciated.
解决方案Firebase doesn't support Date class objects so you need to store them as long/timestamp which you have already done.
1463845578489
is a long which needs to be stored in a long variable and not a Datechange your variable declaration to
private long publishedDate;
Then to convert long to a valid Date object you can use this
Date d = new Date(publishedDate);
这篇关于无法将java.lang.Long类型的对象转换为java.util.Date类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!