【问题标题】:Getting IndexOutOfBoundIndex on removing from string从字符串中删除时获取 IndexOutOfBounds 索引
【发布时间】:2015-09-16 17:45:32
【问题描述】:

我已经做了很长时间了,但仍然无法弄清楚如何从我的字符串中删除“id”。 它使用相同的方法添加到数组列表中,但不会从字符串中删除。 怎么去掉?

travelselectedId.remove(tv.getId());

还请解释一下为什么会抛出这个 IndexOutOfBoundIndex 错误。我知道这与我的 getId() 方法有关,但无法找出确切原因。

 for(int i=1;i<=activityList.size();i++)
        {
            final TextView tv=new TextView(this);
            tv.setId(Integer.parseInt(activityList.get(i-1).get(Table.user_travelactivities.id.toString())));
            tv.setText(activityList.get(i-1).get(Table.user_travelactivities.travelactivity.toString()));
            if(!travelselectedId.contains(tv.getId()))
            {
                
                tv.setBackgroundResource(R.drawable.round_rect_shape);
            tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.plus_profile,0, 0, 0);
            tv.setTextColor(this.getResources().getColor(R.color.tripoto_orange));
            }
            else
            {
                tv.setBackgroundResource(R.drawable.round_filled_rect_shape);
                tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.check,0,0,0);
                tv.setTextColor(NewProfileSettings.this.getResources().getColor(R.color.white));
            }
            tv.setCompoundDrawablePadding(10);
            tv.setTextSize(16f);                
            FlowLayout.LayoutParams params = new FlowLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        
            
        
            params.horizontal_spacing=14;
            params.vertical_spacing=14;
            x1.addView(tv,params);
            tv.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    
                    
                    if (travelselectedId.contains(tv.getId())) {    
                        tv.setBackgroundResource(R.drawable.round_rect_shape);
                        tv.setTextColor(NewProfileSettings.this.getResources().getColor(R.color.tripoto_orange));
                        travelselectedId.remove(tv.getId());// ================= THIS LINE WHAT SHOULD I DO
                        tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.plus_profile,0, 0, 0);
                    }
                    else
                    {
                        tv.setBackgroundResource(R.drawable.round_filled_rect_shape);
                        tv.setTextColor(NewProfileSettings.this.getResources().getColor(R.color.white));
                        
                        tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.check,0,0,0);
                        travelselectedId.add(tv.getId());
                    }
                    
                }
            });
            
                
        }
    }

【问题讨论】:

  • int i=1 不用多看,你可能想要int i=0。或者更大胆的i&lt;=activityList.size()+1;
  • 那无济于事。我已经编辑了问题。
  • 试试travelselectedId.remove(v.getId());

标签: android arraylist indexoutofboundsexception


【解决方案1】:

我假设travelselectedId 是某种java.util.List(例如ArrayList)。

您使用了错误版本的List.remove()。也就是说,您正在调用List.remove(int index)——它会删除具有指定列表索引的项目。相反,您应该调用List.remove(Object obj)——这将删除列表中具有该值的对象。

尝试以下,这将调用remove()的后一个版本:

travelselectedId.remove(Integer.valueOf(tv.getId()));

【讨论】:

  • 当我尝试从列表中删除所有元素时,所有元素都没有被删除
猜你喜欢
  • 2022-06-29
  • 2021-06-30
  • 2023-01-07
  • 2019-12-04
  • 2021-04-09
  • 1970-01-01
  • 1970-01-01
  • 2019-07-22
  • 1970-01-01
相关资源
最近更新 更多