1

I have added a checkbox inside a list view, but i am able to select and unselect only the checkboxes but i cannot select the list item. How to overcome this problem? Any help is appreciated and thanks in advance...

My Code Goes Here

List<String> lst = dh.selectAll();
    lv = (ListView)findViewById(R.id.listView1);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.list,R.id.textViewx,lst);
    lv.setAdapter(adapter);


    lv.setOnItemClickListener(this);

dh.selectall() -> Contains the listarray of items from database; lv -> list view identified I've used array adapter and identified a textview to enter items in listview at last, I've given a clicklistener to the listview by implementing OnItemClickListener.

Pattabi Raman
  • 5,814
  • 10
  • 39
  • 60

2 Answers2

1

with out these two lines the list will display the check box but would not be able to check/unchek

ListView listView = getListView();
mainListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
Pratik
  • 30,639
  • 18
  • 84
  • 159
0

You should use multiple choice listview. Fits your needs perfectly. Here is a link to a good tutorial : http://mubasheralam.com/tutorials/android/how-create-multiple-choice-list

Update 1

listViewObj.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

When you have a checkable item in your listview, the touch on item will not be delivered to item. Just to test, set your checkbox as non clickable. chkox.setClickable(false); and test. The clicks will be delivered to your item.

Update 2

You should use android.R.layout.simple_list_item_multiple_choice for list item.

List<String> lst = dh.selectAll();
lv = (ListView)findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                         android.R.layout.simple_list_item_multiple_choice, lst);

lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setListAdapter(adapter);
Community
  • 1
  • 1
Ron
  • 24,175
  • 8
  • 56
  • 97
  • Thats fine. Just use listview object directly instead of calling `getListView` – Ron Sep 12 '11 at 12:57
  • Add your current code to the question. Which layout id are you passing to your adapter? – Ron Sep 12 '11 at 14:22
  • You should set android:focusable="false" in the XML layout for your checkbox in the list item. Otherwise the checkbox will steal all click events and your list view will not get them. – PacificSky May 22 '12 at 05:03