Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Java ArrayList中删除项时接收IndexOutOfBoundsException_Java_Android_Arraylist - Fatal编程技术网

从Java ArrayList中删除项时接收IndexOutOfBoundsException

从Java ArrayList中删除项时接收IndexOutOfBoundsException,java,android,arraylist,Java,Android,Arraylist,我有一个价值清单。我想在取消选中复选框时删除列表项: ArrayList<SalesRoutes> routeList = new ArrayList<SalesRoutes>(); ArrayList<String> selectedRoutes = new ArrayList<String>(); routeList =getSalesRoute(); for (int i = 0; i < routeList.size(); i++)

我有一个价值清单。我想在取消选中复选框时删除列表项:

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);
                    }
                }
            });
   }
routeList在屏幕上显示路线列表。它从数据库获取路由

路由列表示例:

  Route List
  R0001
  R0002   // selected this one ID is 1
  R0003
正在从选定的路由(1)调用删除

这里selectedRoutes只包含一条记录,这意味着没有索引1

如何删除该对象?

调用ArrayList.remove(int-location)将删除数组中该位置的对象。它不会根据对象的Id值删除对象

看起来您正在将一个对象添加到arraylist的索引位置,该位置由.getRouteCode()返回

但是,您正在尝试删除.getId()返回的索引位置处的对象

两者不同步。

调用ArrayList.remove(int-location)将删除数组中该位置的对象。它不会根据对象的Id值删除对象

看起来您正在将一个对象添加到arraylist的索引位置,该位置由.getRouteCode()返回

但是,您正在尝试删除.getId()返回的索引位置处的对象


两者不同步。

您混合了
ArrayList.remove(int-index)
ArrayList.remove(Object o)
您混合了
ArrayList.remove(int-index)
ArrayList.remove(Object o)

如果将selectedRoutes创建为相同对象的集合
ArrayList selectedRoutes
,将更加容易。由于两个集合都包含对同一对象的引用,因此可以通过引用删除该对象:

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

如果将selectedRoutes创建为相同对象的集合
ArrayList selectedRoutes
,则会容易得多。由于两个集合都包含对同一对象的引用,因此可以通过引用删除该对象:

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

这部分代码存在问题:

selectedRoutes.remove(arg0.getId());
Log.i("remove", routeList.get(arg0.getId()).getRouteCode());
System.out.println("----remove ----" + selectedRoutes);
您暗示的是(通常是错误的)假设,即潜在路线列表中的路线索引与所选路线列表中的路线索引相同。相反,您需要在该索引处的潜在路由列表中获取路由的路由代码,然后遍历所选路由列表(实际上只是包含路由代码的字符串),并删除两个匹配的索引。代码如下所示:

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);
String routeCode=routeList.get(arg0.getId()).getRouteCode();
指数=-1;
对于(int i=0;i-1)
选择路由。删除(索引);
Log.i(“删除”,路由代码);
System.out.println(“----remove----”+selectedRoutes);

这部分代码存在问题:

selectedRoutes.remove(arg0.getId());
Log.i("remove", routeList.get(arg0.getId()).getRouteCode());
System.out.println("----remove ----" + selectedRoutes);
您暗示的是(通常是错误的)假设,即潜在路线列表中的路线索引与所选路线列表中的路线索引相同。相反,您希望在该索引处获取潜在路线列表中的路线代码,然后遍历所选路线列表(实际上只是包含路由代码的字符串),并删除两者匹配的索引。代码如下所示:

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);
String routeCode=routeList.get(arg0.getId()).getRouteCode();
指数=-1;
对于(int i=0;i-1)
选择路由。删除(索引);
Log.i(“删除”,路由代码);
System.out.println(“----remove----”+selectedRoutes);

arg0.getId()
是否在第一个示例中包含
0
,在第二个示例中包含
1
?arg0.getId()将返回复选框id。我有两个数组列表“selectedRoutes”和“routelist”`完全异常是什么?行号、堆栈跟踪、错误消息,请发布所有这些。Haran,检查我编辑的答案。是否
arg0.getId()
在第一个示例中包含
0
,在第二个示例中包含
1
?arg0.getId()将返回复选框id。我有2个arraylist“selectedRoutes”和“routelist”`完全异常是什么?行号、堆栈跟踪、错误消息,请发布所有这些。Haran,检查我编辑的答案。否。'getRouteCode()'包含带有复选框的路由列表。我想为所选复选框值创建另一个arraylist。老实说,我认为您需要阅读Javadoc,以便按索引位置和对象从arraylist添加/删除对象。否。'getRouteCode()'包含带有复选框的路由列表。我想为所选复选框值创建另一个arraylist。老实说,我认为您需要阅读Javadoc,以便按索引位置和对象从arraylist添加/删除对象。我精确地输入了arraylist名称:我有两个arraylist。一个用于显示路由,另一个用于显示所选路由(复选框选定值)我精确地输入了arraylist名称:我有两个arraylist。一个用于显示路线,另一个用于显示选定路线(复选框选定值)谢谢。太棒了。工作正常。我应该在HashMap中设置ArrayList。在Android中,我想设置ArrayList,这就是我选择此项的原因。谢谢。太棒了。工作正常。我应该在HashMap中设置ArrayList。在Android中,我想设置ArrayList,这就是我选择此项的原因。