取消选中复选框android时,ArrayList中的IndexOutOfBoundsException

时间:2015-03-09 09:48:19

标签: android arraylist expandablelistview indexoutofboundsexception expandablelistadapter

ExpandableListviewcheckbox中使用Imageview childview

为了管理其回收属性的checkbox会费状态,我使用了ArrayList

我无法得到错误发生的地方, 请帮助我这个案例

先谢谢

@Override
    public View getChildView(int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

        String childText = (String) getChild(groupPosition, childPosition);  
        Log.e("_childText", "karjeevch "+childText);


        int itemType = getChildType(groupPosition,childPosition);      


        ViewHolder viewHolder = null;
        switch (itemType) {

        case 0:
            viewHolder = null;
            convertView=null;
            if (convertView==null) {

                viewHolder=new ViewHolder();                
                convertView = infalInflater.inflate(R.layout.list_child_shape, null);
                viewHolder.shape_name = (CheckBox) convertView.findViewById(R.id.shape_chk_box);
                //viewHolder.shape_name = (TextView) convertView.findViewById(R.id.shape_chk_box);
                viewHolder.img_shape_icon=(ImageView)convertView.findViewById(R.id.img_shape);                


                imageLoader.DisplayImage("http://rosycontact.com/shashvat/images/"+childText.toLowerCase()+".png", viewHolder.img_shape_icon);                
                Log.e("shape", "karjeevshp "+childText);
                viewHolder.shape_name.setText(childText);
                convertView.setTag(viewHolder);               


               //final TextView shape_name_temp=viewHolder.shape_name;

               viewHolder.shape_name.setChecked(itemChecked.get(childPosition));
               final CheckBox shape_name_temp=viewHolder.shape_name;
              viewHolder.shape_name.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        // TODO Auto-generated method stub

                        int id=buttonView.getId();


                        if (id==R.id.shape_chk_box) {

                            if (shape_name_temp.isChecked()==true) {

                                String shape_str=shape_name_temp.getText().toString();
                                All_link.SHAPE_LIST.add(shape_str);
                                Toast.makeText(_context, shape_name_temp.getText().toString(), Toast.LENGTH_SHORT).show();
                                Log.e("chk_shape", "karjeevch "+shape_name_temp.getText().toString());                              
                            }
                            else{
                                String shape_str=shape_name_temp.getText().toString();
                                All_link.SHAPE_LIST.remove(shape_str);                                                          }
                        }                                                                                           
                    }
                });  

               viewHolder.shape_name.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if(position_of_click.contains (childPosition)){ 
                        position_of_click.remove(childPosition);
                    } 
                    else{ 
                        position_of_click.add(childPosition);
                    }
                }
            });



               if(position_of_click.contains(childPosition)){ 
                   viewHolder.shape_name.setChecked(true);
                }
               else{ 
                   viewHolder.shape_name.setChecked(false);
            }

            }
            else{
                //viewHolder=new ViewHolder();
                viewHolder=(ViewHolder)convertView.getTag();
                imageLoader.DisplayImage("http://rosycontact.com/shashvat/images/"+childText.toLowerCase()+".png", viewHolder.img_shape_icon);                                             
                viewHolder.shape_name.setText(childText);                               
                convertView.setTag(viewHolder);                             
            }
            return convertView;     

当我取消选中复选框时,我在第position_of_click.remove(childPosition);行获得了IndexOutOfBoundsException

1 个答案:

答案 0 :(得分:0)

我认为您的position_of_click变量是ArrayList<Integer>个对象?

在这种情况下,请注意remove()方法:您要使用remove(Object object)并使用remove(int index)。要强制您的代码使用remove(Object object)方法,您必须将您的参数转换为:

position_of_click.remove((Integer)childPosition);