Anush
Anush

Reputation: 53

Checkbox getting deselected in arrayadapter in listview

CheckBox is automatically getting deselected while I scroll down Here is my following code:

  package com.serial.teach;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class TeachAdapter extends ArrayAdapter<String> {
    Context context;
    ArrayList<String> roll =new ArrayList<String>();
    ArrayList<String> checkbox=new ArrayList<String>();
    private int lastPosition = -1;
    private int counter = 0;
    ArrayList<Boolean> status = new ArrayList<Boolean>();
    public static List<Integer> SelectedBox = new ArrayList<Integer>();
    Animation animation;
    View row;
    public TeachAdapter(Context c,ArrayList<String> r, ArrayList<String> ch) {
        super(c, R.layout.single_rows, R.id.name, ch);
        // TODO Auto-generated constructor stub
        this.context = c;
        this.roll = r;
        this.checkbox = ch;
        for (int i = 0; i < roll.size(); i++) {
            status.add(false);
        }

    }

    class MyViewHolder {
        TextView tv;
        CheckBox cb;

        public MyViewHolder(View v) {
            // TODO Auto-generated constructor stub
            tv = (TextView) v.findViewById(R.id.roll);
            cb = (CheckBox) v.findViewById(R.id.name);
        }
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        row = convertView;
        final MyViewHolder holder;
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.single_rows, parent, false);
            holder = new MyViewHolder(row);
            row.setTag(holder);
        } else {
            holder = (MyViewHolder) row.getTag();
        }

        animation = AnimationUtils.loadAnimation(getContext(),
                (position > lastPosition) ? R.anim.up_from_bottom
                        : R.anim.abc_fade_in);
        //down_from_top
        row.startAnimation(animation);
        lastPosition = position;

        holder.tv.setText(roll.get(position));
        holder.cb.setText(checkbox.get(position));
        holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

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

                if (holder.cb.isChecked()) {    
                    status.set(position, true);
                    SelectedBox.add(Integer.parseInt((roll.get(position))));
                    Toast.makeText(getContext(), "You selected " + roll.get(position)+" "+ checkbox.get(position),
                            Toast.LENGTH_SHORT).show();
                    holder.cb.setTextColor(Color.rgb(0, 255, 0));
                } else {
                    status.set(position, false);

                    Toast.makeText(getContext(), "You unchecked " + position,
                            Toast.LENGTH_SHORT).show();
                   holder.cb.setTextColor(Color.rgb(255, 0, 0));

                }
            }
        });
        holder.cb.setChecked(status.get(position));
        holder.cb.setTag(position);
        return row;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return super.getCount();
    }

    @Override
    public String getItem(int position) {
        // TODO Auto-generated method stub
        return super.getItem(position);
    }

}

I have a textView and a checkbox like this:

TV   |_|Checkbox 
TV   |_|Checkbox
TV   |_|Checkbox
TV   |_|Checkbox
TV   |_|Checkbox

But for many items when i select its deselecting automatically.

My layout consists of a textView and a CheckBox as mentioned

Upvotes: 0

Views: 781

Answers (2)

Anirudh Sharma
Anirudh Sharma

Reputation: 7974

this is because the getview is called again and again.So to prevent it you must preserve the state of checkboxes.THIS link helped me and it will also help you.This is the best solution i found.I cannot paste the code because it's too big.It's a complete blog on the this issue

Upvotes: 1

Faruk Yazici
Faruk Yazici

Reputation: 2404

The list elements are viewed by your getView() function in your adapter and they are re-generated when they appear in the visible part of the screen. Your checkBox may be checked, but when it goes out of screen, this information is recycled. When it appears in screen again, the getView() method works again and it appears as unchecked. However, this is just appearance, it means, any programmatic algorithms you make in the background related to checkBox are still valid. If you want to keep the appearances of checkBoxes too, you should keep a list of boolean values that hold status.

For example, create a boolean list checkedBefore. Fill this list accordingly at each checkBox operation. The position parameter in your getView() method is the index in the list. Then you can use the following code to display the checkBoxes properly each time;

if( checkedBefore(position) ){              
    checkBox.setChecked(true);
}

Upvotes: 0

Related Questions