我有一个ArrayList< String>我用来存储PackageInfo(arraylist中元素的一个例子是“com.skype.raider”).
该集会成员的初始化如下:
private List<String> pkgs;
并且在类构造器中
pkgs = new ArrayList<>();
当我调用pkgs.remove(String)时,它不起作用,但是当我反复尝试删除时,它最终会起作用.
private void togglePackage(String selectedPackage,CheckBox chk_app){ String m_pkg = selectedPackage.toString(); //redundant .toString() boolean checked = !chk_app.isChecked(); //checkBox boolean toggle if (checked && !pkgs.contains(m_pkg)) { //if not already in arraylist pkgs.add(m_pkg); //adding the newly checked package } else if (!checked && pkgs.contains(m_pkg)) { //if it needs to be removed pkgs.remove(m_pkg); //<-----------------------This works around the 3rd time i press the checkBox } //Here i check if the string was actually removed from the arrylist //This following code will not be in production,i just used it for testing if (pkgs.contains(m_pkg)) { if (checked) { chk_app.setChecked(checked);//Success } else { chk_app.setChecked(!checked);//Failure } } else { if (!checked) { chk_app.setChecked(checked);//Success } else { chk_app.setChecked(!checked);//Failure } } }
这是未经编辑的onclick事件
RelativeLayout rl_container = (RelativeLayout) child.findViewById(R.id.rl_container); rl_container.setTag(pkg); rl_container.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String m_pkg = v.getTag().toString(); System.out.println("Pkg = "+m_pkg); boolean checked = !chk_app.isChecked(); if (checked && !pkgs.contains(m_pkg)) { pkgs.add(m_pkg); System.out.println("Adding " + m_pkg); } else if (!checked && pkgs.contains(m_pkg)) { pkgs.remove(m_pkg); System.out.println("Removing " + m_pkg); } if (pkgs.contains(m_pkg)) { if (checked) { System.out.println("Success"); chk_app.setChecked(checked); } else { System.out.println("Fail"); chk_app.setChecked(!checked); } } else { if (!checked) { System.out.println("Success"); chk_app.setChecked(checked); } else { System.out.println("Fail"); chk_app.setChecked(!checked); } } } });
作为一个例子,我已经包含了通过迭代列表获得的arraylists内容.
以及我的测试的日志输出
清单内容:
com.sbg.mobile.phone com.google.android.youtube com.e8tracks com.vlingo.midas com.google.android.googlequicksearchBox com.truecaller
当我尝试取消选择“com.sbg.mobile.phone”时,未经编辑的代码输出Logcat
12-18 10:37:25 ViewPostImeInputStage ACTION_DOWN 12-18 10:37:25 Pkg = com.sbg.mobile.phone 12-18 10:37:25 Removing com.sbg.mobile.phone 12-18 10:37:25 Fail 12-18 10:37:28 ViewPostImeInputStage ACTION_DOWN 12-18 10:37:28 Pkg = com.sbg.mobile.phone 12-18 10:37:28 Removing com.sbg.mobile.phone 12-18 10:37:28 Fail 12-18 10:37:30 ViewPostImeInputStage ACTION_DOWN 12-18 10:37:31 Pkg = com.sbg.mobile.phone 12-18 10:37:31 Removing com.sbg.mobile.phone 12-18 10:37:31 Fail 12-18 10:37:32 ViewPostImeInputStage ACTION_DOWN 12-18 10:37:32 Pkg = com.sbg.mobile.phone 12-18 10:37:32 Removing com.sbg.mobile.phone 12-18 10:37:32 Fail 12-18 10:37:33 ViewPostImeInputStage ACTION_DOWN 12-18 10:37:33 Pkg = com.sbg.mobile.phone 12-18 10:37:33 Removing com.sbg.mobile.phone 12-18 10:37:33 Success
PS.这是我的第一个问题,所以要温柔.
我也很感激有关如何改进提问的任何建议,我试图包含所有必需的信息,但如果您还需要其他信息,请告诉我.
解决方法
我认为这可能是个问题,因为ArrayList为您提供了多次存储相等对象的机会.也许,使用HashSet< String>是个好主意.代替?
private Set<String> pkgs;