1

i have a listview with an imageview and a checkbox. Now the issue here is that when i check the first checkbox, and the i scroll down the list, other checkboxes are also getting checked.

For example, if i check the first check box, then the 5th checkbox is also getting checked and so on. Now i have done something in my getView, such that this error is removing, but when i scroll, then again it becomes unchecked. Here is my getView() method

positionArray = new ArrayList<Boolean>(rowItems.size());
    for(int i = 0; i < rowItems.size(); i ++)
        positionArray.add(false);
//------------------------------------------ the above code is in oncreate


public View getView(final int position, View convertView, ViewGroup parent) {

        RowItem rowItem = getItem(position); //List of Items
        ViewHolder holder = new ViewHolder();
        if (convertView == null) {

            LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.list_item, null);
            holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
            holder.radio1 = (CheckBox) convertView.findViewById(R.id.radio);
            convertView.setTag(holder);
        } 
        else{
            holder = (ViewHolder) convertView.getTag();
        }
        //holder.radio1.setChecked(positionArray.get(position));
        holder.radio1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
                if(isChecked)
                    positionArray.set(position, true);


            }
        });
        imageLoader.displayImage(rowItem.getimageUrl(), holder.imageView, options, animateFirstListener);

        return convertView;
    }
Rahul Gupta
  • 5,275
  • 8
  • 35
  • 66

2 Answers2

3

In the getView() You have to update the view, to show if item is selected or not. Just uncomment the line:

holder.radio1.setChecked(positionArray.get(position));

It is important for you to understand why this is happening. Take a look at this answer:

Update: You are not un-setting the value in the array (when isChecked=false)

positionArray.set(position, isChecked);

If above does not work, I would suggest you change your solution based on this method:

Community
  • 1
  • 1
Amulya Khare
  • 7,718
  • 2
  • 23
  • 38
  • It was uncommented before . It is working by doing that. Multiple checkboxes are note getting checked. But when i scroll down and again scroll up to that checkbox , it becomes unchecked again – Rahul Gupta Nov 20 '13 at 07:31
1

Checkboxes not retain it's value on scrolling because ListView create it's view again when we scroll it.

What you can do is : Insert the checked item into an arraylist and then everytime when you check one checkbox change the value inside the arraylist.

If you check any item then add into the arraylist otherwise remove it on Uncheck any item that is early selected inside getview method.

Naresh Sharma
  • 4,323
  • 7
  • 48
  • 68