【发布时间】:2018-04-03 02:12:51
【问题描述】:
我使用 Cloud Firestore 如下:
“事件”集合包含使用唯一事件 ID 作为名称的文档。在这些文档中有许多“EventComment”对象——每个对象代表用户发表的评论。
要将“EventComment”对象添加到文档中,我使用以下内容:
EventComment mcomment = new EventComment();
mcomment.setComment("this is a comment");
Map<String, EventComment> eventMap = new HashMap<>();
eventMap.put(Long.toHexString(Double.doubleToLongBits(Math.random())), mcomment);
firestore.collection("events").document(event_id)
.set(eventMap, SetOptions.merge()).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(EventCommentsActivity.this, "ya bastud", Toast.LENGTH_SHORT).show();
}
});
我创建了一个 HashMap 的 String 和我的对象“EventComment”,然后在文档中设置它。
但是,当我想检索给定文档中包含的所有“EventComment”对象时,我无法将其转换为 EventComment 对象,即我不能这样做:
DocumentReference docRef = db.collection("events").document("vvG17ZfcLFVna8");
docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
EventComment comment = documentSnapshot.toObject(EventComment.class);
}
});
此外,尝试将 documentSnapshot 强制转换为 HashMap 会给出“未经检查的强制转换”消息,并最终失败。这个想法是使用 cmets 来填充 RecyclerView。
我想我可能需要重新构建我存储数据的方式?
任何建议将不胜感激。
干杯
【问题讨论】:
标签: java android firebase google-cloud-firestore