ListView: how can I wait for loading?

I use ListView with ArrayAdapter, and code execution after ListView.setAdapter(ArrayAdapter,...) sees ListView.getChildCount()==0 .

Is there any way t to wait for the UI thread to finish populating the ListView before continuing?

Thanks in advance, Lenny

+4
source share
2 answers

ListView.getChildCount () inherits from ViewGroup and does not apply to elements managed by the adapter, but child views controlled by ListView. To get the number of items displayed in the ListView, use getCount (), which returns the number of items managed by the adapter.

+1
source

I had a similar problem that I decided to create a thread that has loops that can only go after loading the list.

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

This approach can be done because I guarantee that at the moment there is something in the list. Be careful using this approach. This is not the best, but it was the one I found.

0
source

All Articles