最佳答案
正如我在您的数据库架构中看到的,您的 user_stories
属性是数组类型。这意味着,当您尝试获取该文档时,您会以对象的List
形式获取该属性,而不是以数组的形式获取该属性。不幸的是,Firestore 无法将该列表转换为自定义对象列表,您必须自己执行此操作。所以请尝试这个:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference sendStoriesRef = rootRef.collection("sendstories");
sendStoriesRef.document("kueIJcdQARzEKU7EVio9").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
List<Map<String, Object>> list = (List<Map<String, Object>>) document.get("user_stories");
//Iterate through the list to get the desired values
}
}
}
});
现在只需迭代列表即可获取所需的值。如果 kueIJcdQARzEKU7EVio9
是用户的 uid
,则只需使用以下 uid
更改该值即可:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
关于java - 有没有办法从android中的firestore中的数组获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56765444/