0

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

Brandon Bong
  • 51
  • 1
  • 8
  • Is there a reason why you need to use an `ArrayList` and a `HashSet`? If you need to preserver the order of the elements and prevent duplicates, why not just use [LinkedHashSet](https://developer.android.com/reference/kotlin/java/util/LinkedHashSet)? – LuCio Oct 19 '18 at 07:44
  • @LuCio If I dont use the HashSet, each time i check and uncheck a checkbox it will just add on to the list, showing duplicate value. – Brandon Bong Oct 19 '18 at 07:46
  • As far as I can see using a `LinkedHashMap` would reduce the complexity and maybe resolve your issue too since there is no need to determine the proper state using two collections. – LuCio Oct 19 '18 at 07:51
  • I have implemented LinkedHashSet and its less complex now, but it doesnt remove the previously checked value. – Brandon Bong Oct 19 '18 at 08:05
  • If `citiesSelection` is a `LinkedHashMap` thus it contains the names of the cities then `onClick` should look like that: `if (viewHolder.checkBox.isChecked()) { citiesSelection.add(dataModel.getCity()); } else { citiesSelection.remove(dataModel.getCity()); } Toast.make ...` – LuCio Oct 19 '18 at 08:12
  • I have tried that too, it should work in theory but somehow it doesnt. – Brandon Bong Oct 19 '18 at 08:28
  • Did you note that I omiited `"\n"` in my code snippet.? – LuCio Oct 19 '18 at 08:32
  • Owh sorry I didnt notice the \n removed. It is fine now, Thanks a lot! Wonder how a \n affects this. – Brandon Bong Oct 19 '18 at 09:16
  • `"\nLondon".hashCode() != "London".hasCode()` and `"\nLondon".equals("London") == false`. Further reading: [How does a HashMap work?](https://stackoverflow.com/questions/6493605/how-does-a-java-hashmap-handle-different-objects-with-the-same-hash-code) – LuCio Oct 19 '18 at 09:22

1 Answers1

0

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

Brandon Bong
  • 51
  • 1
  • 8
  • The reason is: [see my comment](https://stackoverflow.com/questions/52887739/android-set-doesnt-delete-checked-then-unchecked-value-in-arraylist?noredirect=1#comment92691516_52887739) – LuCio Oct 19 '18 at 09:27