Android Refreshing Textview With Checkbox Onclick?
I'm trying to dynamically update (change color and strikethrough text) a TextView within a ListView when the user checks a CheckBox. My code works somewhat but for some reason on
Solution 1:
Well I solved it, not quite sure what was was going on, maybe someone can explain in detail? Anyhow, the issue was with me declaring the TextView repeatedly within the getView(), simply declaring it as a variable (tv in this case) then calling tv within the method worked. Thanks for the help.
@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;
}
Post a Comment for "Android Refreshing Textview With Checkbox Onclick?"