Android用复选框onClick刷新TextView?
Posted
技术标签:
【中文标题】Android用复选框onClick刷新TextView?【英文标题】:Android refreshing TextView with checkbox onClick? 【发布时间】:2014-11-18 05:05:59 【问题描述】:当用户选中 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;
问题陈述没有变化。
【问题讨论】:
【参考方案1】:好吧,我解决了,不太清楚发生了什么,也许有人可以详细解释一下?无论如何,问题在于我在 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;
【讨论】:
以上是关于Android用复选框onClick刷新TextView?的主要内容,如果未能解决你的问题,请参考以下文章
在 ViewPager 中使用 android:onclick