如何修复我的Arraylist并在ListView中进行设置 [英] How to fix my Arraylist and set it in my ListView

查看:37
本文介绍了如何修复我的Arraylist并在ListView中进行设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我遇到了将Firestore中的值放到我的列表视图中的问题.我在YouTube上观看了一些教程,以了解其工作原理,但不幸的是没有任何工作.

So i'm having the problem of putting the values from the Firestore to my list view. I watched tutorials on Youtube to know how this works but unfortunately nothing is working.

我尝试了一切,但没有任何效果.

I have tried anything but nothing works.

public class MainActivity extends AppCompatActivity {

    ArrayList<String> doctors =new ArrayList<String>();
    ListView txt;

    private FirebaseFirestore db = FirebaseFirestore.getInstance();
    private CollectionReference noteRef = db.collection("Doctors");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        db.collection("Doctors")
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {

                            for (QueryDocumentSnapshot document : task.getResult()) {
                                doctors.add(document.getId());


                            }
                        } else {
                            Toast.makeText(MainActivity.this,"Failed", Toast.LENGTH_LONG).show();

                        }
                    }
                });
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, doctors);

        txt = (ListView)findViewById(R.id.gg1);
        txt.setAdapter(arrayAdapter);
    }
}

推荐答案

尝试移动:

ArrayAdapter<String> arrayAdapter = new 
ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, doctors);

txt.setAdapter(arrayAdapter)

for循环完成后,将

转换为onComplete.那应该起作用.

to onComplete after the for loop is finished. That should work.

这篇关于如何修复我的Arraylist并在ListView中进行设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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