我试图在用户检查CheckBox时动态更新(更改颜色和删除文本)ListView中的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;
}
问题陈述无变化。
答案 0 :(得分:0)
嗯,我解决了它,不太确定发生了什么,也许有人可以详细解释一下?无论如何,问题在于我在getView()中重复声明TextView,只是将其声明为变量(在这种情况下为tv),然后在方法中调用tv。谢谢你的帮助。
@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);
}
final TextView tv = (TextView)view.findViewById(R.id.tvSLItemName);
String anItem = get_Item(position);//from item array
tv.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;
tv.setTextColor(Color.GRAY);
tv.setPaintFlags(
((TextView) aView.findViewById(R.id.tvSLItemName)).getPaintFlags()
| Paint.STRIKE_THRU_TEXT_FLAG);
notifyDataSetChanged();
}
else{
checked[position] = false;
}
}
});
checkBox.setChecked(checked[position]);
return view;
}