从字符串

时间:2015-09-16 17:45:32

标签: android arraylist indexoutofboundsexception

我已经这么久了,仍然无法弄清楚如何从我的字符串中删除“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());
                    }

                }
            });


        }
    }

1 个答案:

答案 0 :(得分:0)

我认为travelselectedId是某种java.util.List(例如,ArrayList)。

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

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

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