【问题标题】:IndexOutOfBoundsException in ArrayList when unchecking checkbox android取消选中复选框android时ArrayList中的IndexOutOfBoundsException
【发布时间】:2015-03-09 09:48:18
【问题描述】:

ExpandableListview 中,我在childview 中使用checkboxImageview

为了管理 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

【问题讨论】:

    标签: android arraylist expandablelistview indexoutofboundsexception expandablelistadapter


    【解决方案1】:

    我想你的 position_of_click 变量是一个 ArrayList<Integer> 对象?

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

    position_of_click.remove((Integer)childPosition);
    

    【讨论】:

    • 嗯好的对不起,所以你可以使用:int index = position_of_click.indexOf(childPosition); position_of_click.remove(index);但请注意,使用这种方法,您只会删除第一次出现的 childPosition 值,但我认为它是 Array 中的唯一值?如果不是:stackoverflow.com/questions/12740701/…
    猜你喜欢
    • 2014-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多