I got a tricky problem here:
in my app, there’s a list (using a ListView) and each item contains text and a checkbox aligned to the right hand side.
Now, clicking the checkbox itself works, I installed a handler (OnClickListener) for it and perform an internal action. The visible state of the checkbox is just as expected – clicked the first time the tick is there, clicked again the tick disappears.
Now I simply wanted to “extend” the clickable area to the complete list entry – my approach was to install an OnItemClickListener using ListView.setOnItemClickListener() and within this listener I do the following:
resultView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox);
checkBox.performClick();
}
});
That sounds simple, but what happens is that sometimes (not every time) the tick in the checkbox does not appear after that! I added a checkBox.invalidate() but that did not help.
How can I have a checkbox “react” to the click of the complete list entry item???
Thanks!
Rather than use
performClick()usecheckBox.setChecked( !checkBox.getChecked() );instead.performClick()will cause theonClickListenerto be called again potentially causing a recursion, which could explain the odd behaviour that you’re seeing.