Spinner NOT Working Well with Firestore's document.getId

安稳与你 提交于 2019-12-13 02:47:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!