从 Java ArrayList 中删除项目时收到 IndexOutOfBoundsException

发布于 2024-12-02 05:53:55 字数 1566 浏览 0 评论 0原文

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

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

这里我得到了 IndexOutOfBoundsException ,因为选中了 selectedRoutes CheckBox 值,

   selectedRoutes   
   [R0002]

routeList 在屏幕上显示路由列表。它从数据库获取路线。

示例routeList:

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

从selectedRoutes(1) 中调用remove。

   selectedRoutes.remove(arg0.getId());

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

如何删除它?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

在风中等你 2024-12-09 05:53:55

问题在于这部分代码:

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

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);
迷雾森÷林ヴ 2024-12-09 05:53:55

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

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

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()));
只等公子 2024-12-09 05:53:55

调用 ArrayList.remove(int location) 将删除数组中该位置的对象。它不会通过 ID 值删除对象。

看起来您正在将一个对象添加到数组列表中 .getRouteCode() 返回的索引位置处,

但随后您试图删除 .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.

昨迟人 2024-12-09 05:53:55

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

You have mixed ArrayList.remove(int index) and ArrayList.remove(Object o).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文