Android 从ArrayList中删除对象

Android 从ArrayList中删除对象,android,arrays,arraylist,Android,Arrays,Arraylist,我有一个ExpandableListView,它将类Group的对象作为组。每个组对象都有ArrayList,该数组中的对象是子对象。当用户单击child时,应该执行以下操作: 检查子对象的布尔值是否已选定设置为true或false。True表示将其添加到数组中,false表示将其从数组中删除 如果为true:检查所选\u组\u ID数组中是否存在子级父级(组)的ID 如果是-将所选子对象添加到数组所选组中的ArrayList中 如果否-将ID添加到数组选定的\u组\u ID,使用子级的父级

我有一个ExpandableListView,它将类Group的对象作为组。每个组对象都有ArrayList,该数组中的对象是子对象。当用户单击child时,应该执行以下操作:

  • 检查子对象的布尔值
    是否已选定
    设置为true或false。True表示将其添加到数组中,false表示将其从数组中删除
  • 如果为true:检查所选\u组\u ID数组中是否存在子级父级(组)的ID
    • 如果是-将所选子对象添加到数组
      所选组中的ArrayList中
    • 如果否-将ID添加到数组
      选定的\u组\u ID
      ,使用子级的父级ID创建新的空组对象,并将其添加到
      选定的\u组
      数组中,将选定的子级添加到该组中的ArrayList中
  • 如果为false:从所选组内的组中的ArrayList中删除此子级
    • 检查ArrayList大小
    • 如果为0-从
      所选的\u组
      数组中删除组对象
下面是我试图做的:

这是组对象代码:

public class Group {

private int group_id;
private String group_name;
private ArrayList<Base_Item> group_items = new ArrayList<Base_Item>();

public Group(int category_id, String group_name, ArrayList<Base_Item> items) {
    super();
    this.group_id = category_id;
    this.group_name = group_name;
    this.group_items = items;
}


public Group(int group_id, String group_name) {
    super();
    this.group_id = group_id;
    this.group_name = group_name;
}

// Getters and setters...

public int remove_item_from_array(Base_Item item)
{
    for(Base_Item current_item : group_items)
    {
        if(current_item.getItem_id() == item.getItem_id())
        {
            group_items.remove(current_item);
        }
    }
    return group_items.size();
}
它指向我的小组课中的这一行(第67行):

我的活动中的这一行(第359行):

第338行:

private void remove_item_from_array(Group g, Base_Item bi)
第114行:

remove_item_from_array(temp_group, bi);

为什么会这样?我在这里做错了什么?

在使用foreach循环时,不能使用collection.remove()函数删除元素,只有迭代器.remove()函数以这种方式删除,而您在这种形式下不可用

因此,问题在于

for(Group current_group : arr_selected_groups) //you are using this kind of loop
{
    ....
}
要使此代码正常工作,请使用以下稍微愚蠢的迭代版本:

for(int i = 0; i < arr_selected_groups.size(); i++)
{
    Group current_group = arr_selected_groups.get(i);
    if(current_group.getGroup_id() == g.getGroup_id())
    {
        int arr_size = current_group.remove_item_from_array(bi);
        if(arr_size==0)
        {
            arr_selected_groups_ids.remove(current_group.getGroup_id());
            arr_selected_groups.remove(current_group);
            i--; //once you remove the current element, decrease the index by one

            //you can also use the line arr_selected_groups.remove(i--);
        }
    }
}
for(int i=0;i
你也可以使用

Iterator<Group> iterator = arr_selected_groups.iterator();
while(iterator.hasNext())
{
    Group current_group = iterator.next();
    if(current_group.getGroup_id() == g.getGroup_id())
    {
        int arr_size = current_group.remove_item_from_array(bi);
        if(arr_size==0)
        {
            arr_selected_groups_ids.remove(current_group.getGroup_id());
            iterator.remove(); //this method 
        }
    }
}
Iterator Iterator=arr_selected_groups.Iterator();
while(iterator.hasNext())
{
Group current_Group=iterator.next();
if(当前组.getGroup\u id()==g.getGroup\u id())
{
int arr\u size=当前\u组。从\u数组(bi)中删除\u项\u;
如果(arr_size==0)
{
arr_selected_groups_id.remove(当前_group.getGroup_id());
迭代器.remove();//此方法
}
}
}

但老实说,我不喜欢它比我提到的第一个,我认为迭代器使它在列表的情况下不那么清楚。对于集合,当然会有所不同。

如果要
修改循环中的集合,则需要使用
迭代器

Iterator it=list.iterator();
while (it.hasNext()){
    System.out.println(it.next());
    it.remove(); // valid here
}

选择simple for循环而不是foreach。可能重复的我试图在我的组类中使用相同的“dumb”循环从数组中删除基本项,但最终得到以下错误消息:java.lang.IndexOutOfBoundsException:无效索引3,大小为2`在此行:
arr_selected_groups_id.remove(current_Group.getGroup_id())
所以我把它改为:
arr\u selected\u groups\u id.remove(arr\u selected\u groups\u id.indexOf(current\u group.getGroup\u id())然后看起来它工作得很好!非常感谢你!!!我一直在为这个问题绞尽脑汁……我很高兴我能帮上忙:)不过,再看一眼,
remove\u item\u from\u array()
中的
group\u items
中的项目移除也有同样的错误,只是等待从深处出现。只是一张便条。根据你的解释,我实际上也立即更改了它。所以它使用了for循环,而不是foreach。运行了一些测试,它成功了!希望它不会失败!:)现在有其他问题,有其他代码,但这是另一回事…:)
for(Group current_group : arr_selected_groups) //you are using this kind of loop
{
    ....
}
for(int i = 0; i < arr_selected_groups.size(); i++)
{
    Group current_group = arr_selected_groups.get(i);
    if(current_group.getGroup_id() == g.getGroup_id())
    {
        int arr_size = current_group.remove_item_from_array(bi);
        if(arr_size==0)
        {
            arr_selected_groups_ids.remove(current_group.getGroup_id());
            arr_selected_groups.remove(current_group);
            i--; //once you remove the current element, decrease the index by one

            //you can also use the line arr_selected_groups.remove(i--);
        }
    }
}
Iterator<Group> iterator = arr_selected_groups.iterator();
while(iterator.hasNext())
{
    Group current_group = iterator.next();
    if(current_group.getGroup_id() == g.getGroup_id())
    {
        int arr_size = current_group.remove_item_from_array(bi);
        if(arr_size==0)
        {
            arr_selected_groups_ids.remove(current_group.getGroup_id());
            iterator.remove(); //this method 
        }
    }
}
Iterator it=list.iterator();
while (it.hasNext()){
    System.out.println(it.next());
    it.remove(); // valid here
}