2

I'm using a listview with checkboxes and set it to multi_choice.

Everything works fine if I'm doing a one time getCheckeItemPositions to get the checked listview items.

However if I do it again and uncheck one of the items, its still counted as checked. I can only add more items to "checked". How to solve this?

sp = new SparseBooleanArray();
        lTransfer = new ArrayList<String>();
        ListView info = (ListView)findViewById(R.id.info);
        sp = info.getCheckedItemPositions();    
        Log.d("Watcher","Arraysize:" + sp.size());
        for(int i = 0; i< sp.size();i++){
            Log.d("Watcher","Arrayfound:" + info.getAdapter().getItem(sp.keyAt(i)).toString().split(":")[0]);
            lTransfer.add(info.getAdapter().getItem(sp.keyAt(i)).toString().split(":")[0]);
        }


public void updateInfo(){
    ListView info = (ListView)findViewById(R.id.info);
    info.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    info.setItemsCanFocus(false);
    info.setOnItemClickListener(new InfoListener());

    lSpin = new ArrayAdapter<String>(this, R.layout.list_item, lToAdd);         
    info.setAdapter(lSpin);   
}
Arian
  • 3,183
  • 5
  • 30
  • 58
  • you are using custom adapter... – Bharat Sharma May 14 '12 at 11:39
  • I have never used ArrayAdapter but used custom adapter alot... I can give you solution if you can tell me two things. First is can you set call back on checked change for your checkbox and second is you have position of checkbox when it is checked or unchecked. – Bharat Sharma May 14 '12 at 11:47
  • I can call setNotifiyOnChange for the adapter – Arian May 14 '12 at 11:54
  • 1
    Have you tried that I dont think it will work. Just take 50 elements in your listview and check elements randomly and then scroll your listview. Check is it resetting checkboxes check or any random check will be there or not.... Actually this problem I have faced in custom adapter. That item is check but it is not returning as checked or Checkboxes get automatically changes their values.http://stackoverflow.com/questions/10190083/how-to-implement-a-button-that-gets-all-checkboxs-state-and-adds-the-value-of-c/10191369#10191369 look at this link once. – Bharat Sharma May 14 '12 at 12:01

2 Answers2

1

The same problem i had faced... So create the layout which looks like multi_choice listview and inflate in your custom adapter and register the events like OnClickListener() listener in the adapter itself..

NullPointerException
  • 3,978
  • 4
  • 34
  • 52
1

I have solved it like this:

for(int i = 0; i< sp.size();i++){
    if(sp.valueAt(i)==true){
                Log.d("Watcher","Arrayfound:" + info.getAdapter().getItem(sp.keyAt(i)).toString().split(":")[0]);
                lTransfer.add(info.getAdapter().getItem(sp.keyAt(i)).toString().split(":")[0]);
                }
}
Arian
  • 3,183
  • 5
  • 30
  • 58