问题
Any idea why this doesn't work?
mFirestore.collection("DR1")
.document(UserID)
.collection("Story")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
spinnerArray.add(String.valueOf(document.getId()));
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
The spinner drop down works (with no default selection, just empty), but on selection, it also does not appear. No error whatsoever. I have setOnItemSelectedListener
to Toast.maketext
on a selection, but nothing appears as well.
But once I add a:
spinnerArray.add("test");
Before the Firestore database call (the for-loop), then everything works fine. (default selection on "test" on the dropdown list, and when I select another entry, Toast.maketext
appears, and selection appears on the spinner)
Thanks again.
回答1:
This is happening because you are not defining your spinnerArray
inside the onComplete()
method, which has an asynchronous behaviour. To solve this, move the declaration of your spinnerArray
inside the onComplete()
method. To display your records you also need to set the adapter inside the method. For more information please see my answer from this post. I also recommend that you see this video, Asynchronous Firebase API - Cloud Firestore and Android, for a better understanding.
来源:https://stackoverflow.com/questions/49233561/spinner-not-working-well-with-firestores-document-getid