当用户选中复选框时,我试图在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;
}
问题陈述没有更改。
我解决了它,不太确定发生了什么,也许有人可以详细解释?无论如何,问题是我在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;
}