使用ListView在setAdapter()后,对ListView的childView做一些操作。但是每次要取得childCount总是0。
原來setAdapter是非同步(asynchronous)。
只要在setAdapter()之后,加入post函式去更新ListView的ChildView即可。
lv = (ListView)findViewById(R.id.list);
lv.setAdapter(adapter);
lv.post(new Runnable(){
public void run(){
//这里获取的getChildCount()就正常了,返回的是当前可见的View的个数,getCount()返回的就是Item一共的个数,这里也可以通过getChildAt()去获取想要的东东了
}
}
这个明白了,再看为啥getChildAt() 得到的是null,因为mChildrenCount是0所以就是空啊!
/**
* Returns the number of children in the group.
*
* @return a positive integer representing the number of children in
* the group
*/
public int getChildCount() {
return mChildrenCount;
}
/**
* Returns the view at the specified position in the group.
*
* @param index the position at which to get the view from
* @return the view at the specified position or null if the position
* does not exist within the group
*/
public View getChildAt(int index) {
if (index < 0 || index >= mChildrenCount) {
return null;
}
return mChildren[index];
}