添加/删除 ArrayList<String> 中的字符串通过复选框 [英] Adding/Removing Strings in an ArrayList&lt;String&gt; by Checkbox

查看:21
本文介绍了添加/删除 ArrayList<String> 中的字符串通过复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次选中或取消选中复选框时,我都会遇到有关添加/删除已添加到 ArrayList 的字符串的问题.

I am running into an issue in regards to adding/removing strings that have been added to an ArrayList everytime a checkbox is checked or unchecked.

The issue seems to be that the string is getting duplicated when more than 1 checkbox is checked.所以如果我要检查第一个框,它会将它添加到数组中.如果我取消选中第一个框,它将从数组中删除它.当我执行第二个复选框时出现问题.当我按下另一个复选框时,它似乎添加了先前的复选框字符串,而不是当前字符串的 2 个.

The issue seems to be that the string is getting duplicated when more than 1 checkbox is checked. So if I was to check the 1st box, it would add it to the array. If I uncheck the first box it will remove it from the array. The issue arises when I do a second check box. When I press another checkbox it appears to add the previous checkboxes string, and than 2 of its current string.

Only the first checkbox checked 
10-29 14:46:10.051: E/PAGES(14154): [alertPage]

2 checkboxes checked
10-29 14:46:10.051: E/PAGES(14154): [alertPage, alertPage, automationPage]

对于每个额外的复选框,这种模式都会继续

and this pattern continues for each additional checkbox

关于为什么会发生这种情况的任何想法?

强文本

    if (mCheckBoxAlerts.isChecked()) {
        mAlertsPermLayout.setVisibility(LinearLayout.VISIBLE);
        pageArray.add("alertPage");
    } else {

        mAlertsPermLayout.setVisibility(LinearLayout.GONE);
        pageArray.remove("alertPage");
    }
    if (mCheckBoxAutomation.isChecked()) {
        mAutomationPermLayout.setVisibility(LinearLayout.VISIBLE);
        pageArray.add("automationPage");
    } else {
        mAutomationPermLayout.setVisibility(LinearLayout.GONE);
        pageArray.remove("automationPage");
    }

这是我如何转换为字符串

        String pageString = pageArray.toString();
        Log.e("PAGES", pageString);

推荐答案

每次添加另一个复选框时,它仍会检查前一个复选框是否被选中.如果是,则将另一个对象相同的字符串添加到数组列表中.您需要检查 arraylist 是否已包含该字符串,如果包含则不要添加.我建议使用 Set 而不是 arraylist.这将不允许重复数组列表.

Every time you are adding another checkbox it is still checking to see if the previous checkbox is checked. If it is it is adding another object same String to the arraylist. You need to check if the arraylist already contains the string then don't add if it does. I would recommend using a Set instead of an arraylist. This will not allow duplicates where arraylist will.

这篇关于添加/删除 ArrayList<String> 中的字符串通过复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆