Checkbox And Setonitemclicklistener Not Working In Android
Solution 1:
To make your listview focusable, remove focus from the items themselves. Add the following after instantiating listview:
listview.setItemsCanFocus(false);
Then add the following to your list_item.xml
<CheckBox
android:id="@+id/lock_File_CheckBox"
android:focusable="false"
android:focusableInTouchMode="false"/>
With this, your setOnItemClickListener()
will get called
Solution 2:
You can get the instance of CheckBox inside onItemClick()
by using setTag()
and getTag()
. You can setTag the CheckBox instance inside your getView()
method as
convertView.setTag(R.id.check, viewHolder.checkbox);
And get the instance inside onItemClick()
using,
CheckBoxcheckbox= (CheckBox) v.getTag(R.id.check);
If you have any further query you can check my blog post
.
Solution 3:
Don't use onClick. Use OnCheckedChange
holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked) {
doSomethingWithCheckedState(compoundButton);
} else {
doSomethingWithUnCheckedState(compoundButton);
}
}
});
Keep in mind that CheckBox inherit from CompoundButton for ICS's switch compatibility.
Solution 4:
Focusable view in the list item prevents the firing of onListItemClick()
in the ListActivity
when the list item is clicked. But the effect of onListItemClick()
can be achieved with OnClickListener
. Read here more about this
Post a Comment for "Checkbox And Setonitemclicklistener Not Working In Android"