Spinner无法与Firestore的document.getId一起很好地工作 [英] Spinner NOT Working Well with Firestore's document.getId

查看:49
本文介绍了Spinner无法与Firestore的document.getId一起很好地工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道为什么这行不通吗?

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());
                    }
                }
            });

微调框下拉列表有效(没有默认选择,只是空白),但是在选择时也不会出现.没有任何错误.我在选择中有 setOnItemSelectedListener Toast.maketext ,但是什么也没有出现.

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.

但是一旦我添加:

spinnerArray.add("test"); 

在Firestore数据库调用(for循环)之前,一切正常.(下拉列表上测试"的默认选择,当我选择另一个条目时,出现 Toast.maketext ,并且选择项出现在微调器上)

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)

再次感谢.

推荐答案

之所以会发生这种情况,是因为您没有在 onComplete()方法中定义 spinnerArray 异步行为.要解决此问题,请将 spinnerArray 的声明移到 onComplete()方法内.要显示您的记录,您还需要在方法内部设置适配器.有关更多信息,请参见此 帖子 .我还建议您观看此视频 异步Firebase API-Cloud Firestore和Android ,以便更好地理解.

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.

这篇关于Spinner无法与Firestore的document.getId一起很好地工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆