2011-12-23 34 views
0

我把這個代碼的onCreate()ListActivity的方法結束得到的ListView孩子的onCreate()

ListView list=getListView(); 
for (int i=0;i<list.getAdapter().getCount();i++) { 
    TextView tv= (TextView) list.getChildAt(i).findViewById(R.id.list_item); 
    tv.setTextColor(Color.RED); //i simplify this string for example 
} 

這是行不通的,因爲ListView中沒有孩子的,但 - 它的大小爲0;但.getAdapter()。getCount()返回不爲0.

我應該用什麼來代替list.getChildAt(i)來使此代碼正確?


更新:我想,這是我需要的Formatting ListView Content During onCreate() method (getting ListView children) 但我不明白這個問題

回答

2

視圖被添加到適配器的解決方案,因此,而不是list.getChildAt(i)使用lv.getAdapter().getView(i, null, null)得到視圖

+0

所以我改變碼本: '爲(INT I = 0; I user1049280 2011-12-23 14:14:27

+0

更好的創建你的自定義adapeter和getView()方法。 – 2011-12-23 14:29:49

+0

好的,謝謝你的幫助 – user1049280 2011-12-23 14:33:53