I am coding an android app using Google's FireStore backend. The code to grab all the documents in a firestore collection is as follows, from the official documentation:
db.collection("cities")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d(TAG, document.getId() + " => " + document.getData());
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
Where the above code outputs to Log.d(...)
, I would like to have my program add the results of the document.getData()
call to an ArrayList
accessible outside the inner class/method. I'm not sure what is the best way to do this. Attempting to change the return the return type of the onComplete
method yields errors. Is there a standard way of accessing elements in methods like this?
Declaring a variable and trying to mutate within the class also isn't possible, unless the variable is final, which defeats the point.