添加/在一个ArrayList&LT删除字符串,字符串>通过复选框 [英] Adding/Removing Strings in an ArrayList<String> by Checkbox

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

问题描述

我遇到了这样一个问题:关于添​​加/删除已添加到一个ArrayList每次一个复选框被选中或取消选中的字符串。

这个问题似乎是,当超过1复选框被选中的字符串复制得到。所以,如果我是检查1中,将它添加到阵列中。如果我取消了第一个框会从数组中删除。当我做了第二个复选框的问题就出现了。当我preSS另一个复选框,它似乎增加previous复选框的字符串,比当前字符串2。

 只有第一个复选框选中
10-29 14:46:10.051:E /页(14154):alertPage]2勾选复选框
10-29 14:46:10.051:E /页(14154):alertPage,alertPage,automationPage]

和这种模式继续下去,每增加复选框

任何想法,这是为什么发生?

强文

 如果(mCheckBoxAlerts.isChecked()){
        mAlertsPermLayout.setVisibility(LinearLayout.VISIBLE);
        pageArray.add(alertPage);
    }其他{        mAlertsPermLayout.setVisibility(LinearLayout.GONE);
        pageArray.remove(alertPage);
    }
    如果(mCheckBoxAutomation.isChecked()){
        mAutomationPermLayout.setVisibility(LinearLayout.VISIBLE);
        pageArray.add(automationPage);
    }其他{
        mAutomationPermLayout.setVisibility(LinearLayout.GONE);
        pageArray.remove(automationPage);
    }

这里是我如何转换成字符串

 字符串pageString = pageArray.toString();
        Log.e(页,pageString);


解决方案

要添加其他复选框,它仍然是检查,看看是否previous复选框被选中每次。如果它被添加另一个对象相同的字符串到ArrayList。你需要检查,如果ArrayList中已经包含字符串,然后如果它不添加。我会建议使用一组,而不是一个ArrayList。这将不允许重复的地方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. 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

Any ideas as to why this is occurring?

strong text

    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");
    }

Here is how I am converting to a string

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

解决方案

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&LT删除字符串,字符串>通过复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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