java – ArrayList. Remove does not work the first time it is called
I have an ArrayList < string > I use to store packageinfo (an example of an element in ArrayList is "com. Skype. Raider")
The initialization of the assembly members is as follows:
private List<String> pkgs;
And in the class constructor
pkgs = new ArrayList<>();
When I call Pkgs When I remove (string), it doesn't work, but when I try to delete it again and again, it will eventually work
Heirs: how do I test whether the deletion is valid (I edited the code to make it easier to read)
private void togglePackage(String selectedPackage,Check@R_867_2419@ chk_app){ String m_pkg = selectedPackage.toString(); //redundant .toString() boolean checked = !chk_app.isChecked(); //check@R_867_2419@ 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 check@R_867_2419@ } //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 } } }
This is an unedited onclick event
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); } } } });
As an example, I have included the ArrayLists obtained from the iteration list And log output of my tests
List content:
com.sbg.mobile.phone com.google.android.youtube com.e8tracks com.vlingo.midas com.google.android.googlequicksearch@R_867_2419@ com.truecaller
When I try to deselect "com. SBG. Mobile. Phone", the unedited code outputs 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. this is my first question, so be gentle I am also grateful for any suggestions on how to improve the questions. I try to include all the necessary information, but if you need other information, please let me know
Solution
I think this may be a problem because ArrayList gives you the opportunity to store equal objects multiple times Maybe it's a good idea to use HashSet < string > Replace?
private Set<String> pkgs;