Derek
Derek

Reputation: 1572

Disabling interactivity for CheckBoxes in ListView

I'm trying to have a ListView where each row has some text and a checkbox. I know that this is a solved problem, but I tried to do it myself anyways and am very close to getting it working.

Here's my demo app on github.

Clicking on a ListView item anywhere other than the checkbox correctly registers that the box is checked. Clicking on the box doesn't cause an OnItemClick event and thus the fact that the box is checked isn't registered.

I could add a listener to each checkbox and maintain state inside of the adapter which would be nice, but I need to have some way of letting the HomeActivity know when the list has applications checked and when it doesn't (to enable/disable some buttons). I'm considering using a Callback for that, but I've never done it in Java.

To address some common topics in this problem:

Upvotes: 0

Views: 2610

Answers (3)

AnupamChugh
AnupamChugh

Reputation: 1899

Disabling checkbox clickable inside the getView() method as

((CheckBox) result.findViewById(R.id.checkBox)).setClickable(false);

works for me.

Upvotes: 1

A. Abiri
A. Abiri

Reputation: 10810

This will get all the views in your listview and will get the checkbox in each view and adds it to a list if it is checked:

List<CheckBox> listOfCheckboxes = new ArrayList<CheckBox>();
List<View> listOfViews = new ArrayList<View>();
listView1.reclaimViews(listOfViews);
for (View v : listOfViews)
{
    CheckBox cb = (CheckBox)v.findViewById(R.id.checkbox);
    if (cb.isChecked())
    {
        listOfCheckboxes.add(cb);
    }
}

Upvotes: 0

viv
viv

Reputation: 6177

Since you have set checkbox focusable and focusableInTouchMode as false, it will not get any event. See either click listener of list will work or that of checkbox, you cannot have both of them working at the same time.

What you can do is that, attach the listener in bindView on the view, and also on the checkbox. In this way you can get the listener call in the HomeActivity when either row or checkbox is clicked...

You can create interface for it..........

Public Interface MyClickListener{
    public void onClick();

}

Create object MyClickListener object;
object.onClick();

Upvotes: 0

Related Questions