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.
You should update the view based on your data representation. However you are updating view based on checkbox view state that is never set before.
So you should first update checkbox based on portion of data that related to the position and after use it further
You can define interface inside adapter to listen checkbox click and use setTag() on Checkbox and implements that interface on Fragment or Activity and define your definition inside activity or fragment .
If you debug your code, I think if(holder.checkbox.isChecked()) {
this line will always return false
. That is because you have overriden the default on-click event of the CheckBox
, which should be toggle the check status when clicking it, however after your overriden, you ignored to toggle the check status, that's why the background cannot be changed to RED even after you issued notifyDatasetChanged
.
So please try to add holder.checkbox.setChecked(!checkbox.isChecked());
to the first line of your OnClickListener
to see if it helps.
android:clickable="true" please add this tour check box in the xml then you try with your code , when ever you do some operation on adapter you need update with this .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With