我希望在活动创建时将SpannableString设置为ListView的某个项目 我这样写:
doListlist=(ListView)findViewById(R.id.dolist_List);
listsDbAdapter=new yueDataBaseAdapter(this);
listsDbAdapter.open();
UpdataAdpter();
for(int i = 0; i < doListlist.getChildCount(); i++) {
if (listsDbAdapter.fetchData(tableName, i+1).getString(7).length() == 4) {
View view = doListlist.getChildAt(i);
String drawText=listsDbAdapter.fetchData(tableName,i+1).getString(1);
TextView txtInfo =(TextView)view.findViewById(R.id.doList_itemname);
SpannableString ss = new SpannableString(drawText);
ss.setSpan(new StrikethroughSpan(), 0, drawText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
txtInfo.setText(ss);
}
}
public void UpdataAdpter(){
Cursor cur= listsDbAdapter.fetchAllData(tableName);
if(cur!=null&& cur.getCount()>=0){
adapter =new SimpleCursorAdapter(this,
R.layout.dolist_item,
cur,
new String[]{yueDataBaseAdapter.KEY_NAME,yueDataBaseAdapter.KEY_PRICESTR,yueDataBaseAdapter.KEY_NUMSTR},
new int[]{R.id.doList_itemname,R.id.doList_itemPrice,R.id.doList_itemNum}
);
doListlist.setAdapter(adapter);
}
}
但我发现doListlist.getChildCount()始终= 0 问题在哪里?或者其他更好的方法吗?
答案 0 :(得分:0)
因为您还没有设置列表的适配器。做
doListlit.setAdapter(listsDbAdapter);
首先,在致电getChildCount
之前。
答案 1 :(得分:0)