1

How to implement a custom code whenever an item in a multiple choice list view is a listview is checked or unchecked? I couldn't find any suitable article on this.Any help will be much appreciated. Thank you. The following is my code for multiple choice list view.

ListView lv = (ListView) findViewById(R.id.listview);
ArrayList<String> stateList =Handler.getList();
String[] catalogList = new String[stateList.size()];
catalogList=stateList.toArray(catalogList);
lv.setAdapter(new ArrayAdapter<String>(Catalog.this,
android.R.layout.simple_list_item_multiple_choice, catalogList));

2 Answers2

0

Check the useful tutorial link below for generic MultipleChoice ListView. That should work for your case as well, I believe.

http://www.marvinlabs.com/2010/10/custom-listview-ability-check-items/

Korhan Ozturk
  • 11,148
  • 6
  • 36
  • 49
0

You can add onItemClickListener to your listview , on item click check the state of you checkbox and do action you want.

    ListView lv = (ListView) findViewById(R.id.listview);
    ArrayList<String> stateList =Handler.getList();
    String[] catalogList = new String[stateList.size()];
    catalogList=stateList.toArray(catalogList);
    lv.setAdapter(new ArrayAdapter<String>(Catalog.this,
    android.R.layout.simple_list_item_multiple_choice, catalogList));
    lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            CheckedTextView checkedTextView = 
                    (CheckedTextView) view.findViewById(android.R.id.text1);
            System.out.println(
                    checkedTextView.getText()+":"+
                    !checkedTextView.isChecked() //because the onItemClick fired before checkedTextView change its state 
                    );
        }
        });

for why see this

you also can do it by implement custom adapter. a dirty example following:

public class CheckListAdapter extends BaseAdapter {
    //omitted
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

     final int pos = position; // if you need positon in this item's action
     View item = mInflater.inflate(R.layout.list_item_checkboxt, null);
     (CheckBox)item.findViewById(R.id.checkbox)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // do your action here
        }
    });     
  }
 }

more effective of use adapter take a look this post!

DouO
  • 4,914
  • 1
  • 20
  • 29
  • I used this code on a fast Dual Core android and the code broke !checkedTextView.isChecked() because this no longer held true: "onItemClick fired before checkedTextView" seems to be different on different speed androids – hamish May 26 '14 at 08:26
  • DuoO that is not the answer. this code relies on the speed of the event. bad programming. this part doesn't work. you have a major timing bug with this.. it works on slower devices, but doesn't work on faster devices.. !checkedTextView.isChecked() //because the onItemClick fired before checkedTextView change its state – hamish Jun 04 '14 at 01:39
  • Hi, @hamish I don't think it have a timing bug, all UI operation run on UI thread, single thread shouldn't have a multi-thread bug.This code is too old I sure it work on gingerbread but may not on newer devices. If you have a API >= 11, I suggest you use [CAB](http://theopentutorials.com/examples/android/listview/android-contextual-action-bar-for-listview-item-deletion-using-actionbarsherlock/) – DouO Jun 04 '14 at 07:30
  • take a look at what Shai is doing here... i think he is doing extra things that you missed... http://stackoverflow.com/questions/8841283/gmail-like-listview-with-checkboxes-and-using-the-actionbar – hamish Aug 03 '14 at 03:31