Reputation: 5720
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);
}
}
I want to retrieve it in my custom list object let's say List<TaskItem> mList;
which contains all Firestore data.
I tried following way and it shows no such document.
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());
}
}
});
What am I missing here? Any reference or help
Upvotes: 2
Views: 2670
Reputation: 138824
To solve this, please use the following code:
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());
}
}
});
The list
contains now, all your TaskItem
objects.
Upvotes: 3