Arian
Arian

Reputation: 3241

Update getCheckedItemPositions()

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

Upvotes: 2

Views: 698

Answers (2)

Arian
Arian

Reputation: 3241

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

Upvotes: 1

NullPointerException
NullPointerException

Reputation: 4008

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..

Upvotes: 1

Related Questions