0

In ExpandableListview I am using checkbox with Imageview in childview

For managing the state of checkbox dues to its recycling property I used ArrayList

I am not able to get where the mistake is happening , Pls help me in this case

Thanks in Advance

@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;     

I am getting IndexOutOfBoundsException at line position_of_click.remove(childPosition); when I uncheck the checkbox

1 Answer 1

0

I suppose your position_of_click variable is an ArrayList<Integer> object ?

In this case, take care with the remove() method : you want to use remove(Object object) and you are using remove(int index). To force your code to use the remove(Object object) method, you must cast your argument like :

position_of_click.remove((Integer)childPosition);
Sign up to request clarification or add additional context in comments.

1 Comment

Mmh ok sorry, so you can use : int index = position_of_click.indexOf(childPosition); position_of_click.remove(index); But take care, with this method you will remove only the first occurence of your childPosition value, but I think it's a unique value in your Array ? If not : stackoverflow.com/questions/12740701/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.