0
我想動態更新(更改顏色和刪除線文本)當用戶檢查CheckBox時ListView內的TextView。Android使用複選框onClick刷新TextView?
我的代碼在某種程度上起作用,但由於某種原因,只有在複選框第二次點擊時,即用戶檢查沒有任何反應,用戶取消檢查並再次檢查TextView然後根據需要更新?
任何幫助表示讚賞。
我的代碼:
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.shoppinglist, parent, false);
}
String anItem = get_Item(position);//from item array
((TextView) view.findViewById(R.id.tvSLItemName)).setText(anItem);
final CheckBox checkBox = (CheckBox) view.findViewById(R.id.cbBoxSL);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
View aView = convertView;
if (aView == null) {
aView = lInflater.inflate(R.layout.shoppinglist, parent, false);
}
if(isChecked){
checked[position] = true;
((TextView) aView.findViewById(R.id.tvSLItemName)).setTextColor(Color.GRAY);
((TextView) aView.findViewById(R.id.tvSLItemName)).setPaintFlags(
((TextView) aView.findViewById(R.id.tvSLItemName)).getPaintFlags()
| Paint.STRIKE_THRU_TEXT_FLAG);
//aView.invalidate();
notifyDataSetChanged();
}
else{
checked[position] = false;
}
}
});
checkBox.setChecked(checked[position]);
return view;
}
更新後的代碼如下建議:
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.shoppinglist, parent, false);
}
String anItem = get_Item(position);//from item array
((TextView) view.findViewById(R.id.tvSLItemName)).setText(anItem);
final CheckBox checkBox = (CheckBox) view.findViewById(R.id.cbBoxSL);
checkBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
View aView = convertView;
if (aView == null) {
aView = lInflater.inflate(R.layout.shoppinglist, parent, false);
}
if(((CheckBox) v).isChecked()) {
checked[position] = true;
((TextView) aView.findViewById(R.id.tvSLItemName)).setTextColor(Color.GRAY);
((TextView) aView.findViewById(R.id.tvSLItemName)).setPaintFlags(
((TextView) aView.findViewById(R.id.tvSLItemName)).getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
//aView.invalidate();
notifyDataSetChanged();
}else{
checked[position] = false;
}
}
});
checkBox.setChecked(checked[position]);
return view;
}
沒有改變問題的陳述。