Android ListView with Multiselect not deselecting items

111 views Asked by At

I have a multi-select listview where I want to keep track of what the user has selected and what they have deselected. When a user clicks on a list item, I add the clicked items into an ArrayList and when they click it again I remove the item from the ArrayList. The issue is that the deselect operation(when a user clicks on an item already clicked on earlier) is adding into the ArrayList again instead of removing. Below is the code for onListItemClick for that activity:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        CheckBox checkBox = (CheckBox) view.findViewById(R.id.chk_forEasyPayListItem);
        int stringPos = 0;
        selectedBiller = billerNamesArray[position];
        selectedBillerAccountNumber = billerAccountsArray[position];
        selectedBillerBalance = billerToPayBalances[position];

        SparseBooleanArray sparseBooleanArray = lstPayMyBills.getCheckedItemPositions();
        if (sparseBooleanArray != null && sparseBooleanArray.size() > 0) {
            for (int index = 0; index < sparseBooleanArray.size(); index++) {
                if (sparseBooleanArray.valueAt(index)) {
                    lstPayMyBills.setItemChecked(sparseBooleanArray.keyAt(index), true);
                    checkBox.setChecked(true);
                    selectedItems.add(new PayMyBillsListItemModel(position, selectedBiller, selectedBillerAccountNumber, selectedBillerBalance));
                    adapter.notifyDataSetChanged();
                    Toast.makeText(getApplicationContext(), "Array size is " + selectedItems.size(), Toast.LENGTH_LONG).show();
                } else {
                    lstPayMyBills.setItemChecked(sparseBooleanArray.keyAt(index), false);
                    checkBox.setChecked(false);
                    //selectedItems.remove(new PayMyBillsListItemModel(position, selectedBiller, selectedBillerAccountNumber, selectedBillerBalance));
                }
            }
        } else {
            for (int index = 0; index < lstPayMyBills.getCount(); index++) {
                lstPayMyBills.setItemChecked(index, true);
                checkBox.setChecked(false);
                selectedItems.remove(new PayMyBillsListItemModel(position, selectedBiller, selectedBillerAccountNumber, selectedBillerBalance));
                adapter.notifyDataSetChanged();
                Toast.makeText(getApplicationContext(), "Array size is " + selectedItems.size(), Toast.LENGTH_LONG).show();
            }
        }          
    }
1

There are 1 answers

2
Hughzi On

I think your problem may be that you are creating a new object to remove. if you had a list of "A", "B", "C", "D", "B" and you called remove("B") your list would be:

"A", "C", "D", "B" (removes first occurrence). I think you would have to look for the object in your selected list which matches the one deselected then remove that.

I am not sure on the behavior removing a new object?