1

I have added only checkboxes in my ListView.
list_checkbox.xml:
<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="42px">
</CheckBox>

In java; to capture event on list i have used this::

items1={" "," "," "," "}; //blank i.e. no text near checkbox

lv1.setAdapter(new ArrayAdapter<String>(this, R.layout.list_checkbox, items1));
lv1.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

      // When clicked,put code here.....
    }
  });

but now where and how to capture the checkbox event? in detail plz.
i am a newbie.
thanx..

cooldeep
  • 39
  • 2
  • 7
  • You've answered yourself. In the onItemClick you have commented your answer right there. The next question is what do you want to do when an item is clicked. – apesa Apr 03 '11 at 16:43
  • ya thats for when an item is clicked in list. i want to capture for a checkbox. when first checkbox is checked call function A if third is checked call fuction B and so on.. – cooldeep Apr 06 '11 at 12:09

1 Answers1

6

If you put in the ListView either focusable or clickable Views then the your OnItemClickListener won't get called. Checkbox is clickable by default so setting OnItemClickListener in this case won't do anything.

What you can do in this case is to implement a custom Adapter (just extend ArrayAdapter) and add an OnClickListener to every View you provide for the ListView.

public class TestAdapter extends ArrayAdapter {
    ...
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = super.getView(position, convertView, parent);
        v.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // Do your logic here eg. if ((CheckBox)v).isChecked()...
             }
         });
         return v;
    }
}

You can find a very good tutorial on this topic here. It's worth reading the whole chapter but the answer to your question is on page 112, Interactive Rows.

Also look at this Stack Overflow question.

Community
  • 1
  • 1
balazsbalazs
  • 4,071
  • 1
  • 24
  • 29