简体   繁体   中英

Problem with adding checkbox inside a listview

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.

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);

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM