Collection
I have succeed in pushing data as following way:
private void onAddItemsClicked() {
// Get a reference to the restaurants collection
CollectionReference quotes = mFirestore.collection("quotes");
for (int i = 0; i < 10; i++) {
// Get a random Restaurant POJO
TaskItem item = new TaskItem();
item.setId(UUID.randomUUID().toString());
item.setTitle("good life is key to success " + i);
item.setCategory("Life " + i);
// Add a new document to the restaurants collection
quotes.add(item);
}
}
我想在我的自定义列表对象中检索它,比如说 List<TaskItem> mList;
其中包含所有 Firestore 数据。
我尝试了以下方法,但没有显示此类文档。
mQuery = mFirestore.collection("quotes");
mQuery.document().get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document != null && document.exists()) {
Log.d("TAG", "DocumentSnapshot data: " + document.getData());
} else {
Log.d("TAG", "No such document");
}
} else {
Log.d("TAG", "get failed with ", task.getException());
}
}
});
我在这里缺少什么?任何引用或帮助
最佳答案
要解决此问题,请使用以下代码:
mQuery.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
List<TaskItem> list = new ArrayList<>();
for (DocumentSnapshot document : task.getResult()) {
TaskItem taskItem = document.toObject(TaskItem.class);
list.add(taskItem);
}
Log.d(TAG, list.toString());
}
}
});
list
现在包含所有 TaskItem
对象。
关于java - Android FireStore 以列表形式检索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49568811/