简体   繁体   中英

Android Set doesnt delete checked then unchecked value in arraylist

I have an array list that contains all the checked items and a hashset to prevent duplications. But now I have a problem that the array list still stores the previously checked items even though i have unchecked it.

I have looked for solutions like Hashset and using (!isChecked) but those wont delete the previously stored value.

       viewHolder.checkBox.setTag(dataModel.getChkBool());


    viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
                    if (viewHolder.checkBox.isChecked()) {
                        citiesSelection.add("\n" + dataModel.getCity());
                    }

            //prevent duplicates
             hashSet.addAll(citiesSelection);
             citiesSelection.clear();
             citiesSelection.addAll(hashSet);

            Toast.makeText(getContext(), "Checked Total: " + citiesSelection, Toast.LENGTH_LONG).show();
        }

    });

Screenshots:

Checked boxes

Unchecked, but the arraylist still contains the previously checked ones

For some reason, removing the \\n and using citiesSelection.remove(dataModel.getCity()); fixes it thanks to LuCio

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