天天看点

Android - ListView在setAdapter()后,getChildCount总是0的原因

使用ListView在setAdapter()后,对ListView的childView做一些操作。但是每次要取得childCount总是0。

原來setAdapter是非同步(asynchronous)。

只要在setAdapter()之后,加入post函式去更新ListView的ChildView即可。 

<code>lv = (ListView)findViewById(R.id.list);</code>

<code>lv.setAdapter(adapter);</code>

<code>lv.post(</code><code>new</code> <code>Runnable(){</code>

<code>    </code><code>public</code> <code>void</code> <code>run(){</code>

<code>        </code><code>// fileList为与adapter做连结的list总数            </code>

<code>        </code><code>if</code> <code>(fileList.size()==lv.getChildCount()){ </code>

<code>            </code><code>//对ListView中的ChildView进行操作。。。      </code>

<code>        </code><code>}  </code>

<code>    </code><code>}</code>

<code> </code> 

<code>}</code>

本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2013/06/06/3120408.html,如需转载请自行联系原作者

继续阅读