I have a Android ListView, which contains one button thats needs to be checked when clicked. The click event seems to be working:
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = this._context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.checkbox_cell, null);
holder = new ViewHolder();
holder.checkbox = (CheckBox) convertView.findViewById(R.id.button);
holder.textView = (TextView) convertView.findViewById(R.id.textView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(holder.checkbox.isChecked()) {
convertView.setBackgroundColor(Color.RED);
holder.textView.setBackgroundColor(Color.RED);
convertView.invalidate();
} else {
convertView.setBackgroundColor(Color.GREEN);
}
holder.checkbox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Arrays.sort(_context.checkBoxIds);
int index = Arrays.binarySearch(_context.checkBoxIds, position);
if (((CheckBox) v).isChecked()) {
holder.textView.setPaintFlags(holder.textView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
} else {
holder.textView.setPaintFlags(holder.textView.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
}
holder.textView.setBackgroundColor(Color.RED);
notifyDataSetChanged();
}
});
return convertView;
}
As you probably can see i'm changing the background to green initial, when i click the "checkbox" the background needs to be red (this is just a test to see if the UI updates). I tried to update the views in the onClick
, but thats not working either. After the onClick
i call notifyDataSetChanged()
to see if the UI changes when i refresh. The holder.checkbox.isChecked()
seems to be true but when i set the background to RED the UI won't update. Does anyone know what goes wrong here? I'm clueless at the moment.