简体   繁体   中英

Receiving IndexOutOfBoundsException when removing item from Java ArrayList

I have a list of values. I want to remove the list item when check box is unclicked:

ArrayList<SalesRoutes> routeList = new ArrayList<SalesRoutes>();
ArrayList<String> selectedRoutes = new ArrayList<String>();
 routeList =getSalesRoute();
for (int i = 0; i < routeList.size(); i++) {
         CheckBox ch = new CheckBox(this);
   ch.setId(i);
   ch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                    if(arg0.isChecked()){   
                        Log.i("add", routeList.get(arg0.getId()).getRouteCode());
                        selectedRoutes.add(routeList.get(arg0.getId()).getRouteCode());
                        System.out.println("----add ----" + selectedRoutes);
                    }else {
                        selectedRoutes.remove(arg0.getId());
                        Log.i("remove", routeList.get(arg0.getId()).getRouteCode());
                        System.out.println("----remove ----" + selectedRoutes);
                    }
                }
            });
   }

Here I got IndexOutOfBoundsException because selectedRoutes is selected CheckBox values

   selectedRoutes   
   [R0002]

routeList displays a list of routes on the screen. It fetches the route from a db.

Example routeList:

  Route List
  R0001
  R0002   // selected this one ID is 1
  R0003

calling remove from selectedRoutes(1).

   selectedRoutes.remove(arg0.getId());

Here selectedRoutes only contains one record, which means there is no index 1.

How can I remove this?

The problem is with this section of code:

selectedRoutes.remove(arg0.getId());
Log.i("remove", routeList.get(arg0.getId()).getRouteCode());
System.out.println("----remove ----" + selectedRoutes);

You're hinging that on the (usually incorrect) assumption that the index of your route in your list of potential routes is the same as the index of your route in the list of selected routes. Instead, you'll want to get the route code of the route in your potential list of routes at that index, then iterate through your list of selected routes (which are actually just Strings containing route codes), and remove the index where the two match up. Code would look something like this:

String routeCode = routeList.get(arg0.getId()).getRouteCode();
index = -1;
for(int i = 0; i < selectedRoutes.size(); i++) {
    if(routeCode.equals(selectedRoutes.get(i)) {
        index = i;
            break;
    }
}
if(index > -1)
    selectedRoutes.remove(index);
Log.i("remove", routeCode);
System.out.println("----remove ----" + selectedRoutes);

It would be much easier if you created selectedRoutes to be a collection of the same objects ArrayList<SalesRoutes> selectedRoutes . Since both collections would contain the references to the same object you could remove the object by its reference:

salectedRoutes.remove(routeList.get(arg0.getId()));

Calling ArrayList.remove(int location) will remove the object at that location in the array. It does not remove a object by it's Id value.

It appears you are adding an object to the arraylist at an index position returned by .getRouteCode()

But then you are trying to remove an object at an index position returned by .getId().

The two don't sync up.

您已经混合使用ArrayList.remove(int index)ArrayList.remove(Object o)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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