简体   繁体   English

Android Set不会删除arraylist中已选中然后未选中的值

[英]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. 我一直在寻找像Hashset这样的解决方案并使用(!isChecked),但是这些解决方案不会删除以前存储的值。

       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 未选中,但arraylist仍包含以前选中的数组

For some reason, removing the \\n and using citiesSelection.remove(dataModel.getCity()); 由于某些原因,删除\\ n并使用citySelection.remove(dataModel.getCity()); fixes it thanks to LuCio 借助LuCio对其进行了修复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM