简体   繁体   English

ListView:如何等到它加载完毕?

[英]ListView: how do I wait until it's loaded up?

I'm using a ListView with an ArrayAdapter, and code executing after ListView.setAdapter(ArrayAdapter,...) is seeing ListView.getChildCount()==0 . 我正在使用带有ArrayAdapter的ListView,并且在ListView.setAdapter(ArrayAdapter,...)之后执行的代码正在查看ListView.getChildCount()==0

Is there a way t wait for the UI thread to finish filling the ListView before continuing? 有没有办法等待UI线程在继续之前完成填充ListView?

Thanks in advance, Lenny 提前谢谢,莱尼

ListView.getChildCount() is inherited from ViewGroup and is not related to the items managed by the adapter but the child views managed by the ListView. ListView.getChildCount()继承自ViewGroup,与适配器管理的项目无关,但与ListView管理的子视图无关。 To get the number of items displayed by the ListView use getCount() which returns the number of items managed by the adapter. 要获取ListView显示的项目数,请使用getCount()返回适配器管理的项目数。

I had a similar problem that I solved creating a thread that has while loops that can only go forward after the list is loaded. 我有一个类似的问题,我解决了创建一个具有while循环的线程,该循环只能在列表加载后继续。

new Thread(new Runnable() { 
    public void run() { 
       while(listView.getCount() == 0) ;
    } 
}).start(); 

This approach can be done cuz I garantee that at this point there is something in the list. 这种方法可以完成,因为我保证在这一点上列表中有一些东西。 Be carefull using this approach. 小心使用这种方法。 It is not the best one but it was the one that I found. 它不是最好的,但它是我找到的那个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM