[Solved] Checkbox is unchecked whenever listview is scrolled, android?


Your problem is the last for loop. Each row will get the state of the last checked[] array entry.

You should see that if you change the value of the last one.

PS: Your code is a bit “messy” and can be optimized:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    final ViewHolder holder;
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater)
                parent.getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        vi = mInflater.inflate(R.layout.element_dialog_check, null);
        holder = new ViewHolder();
        holder.tvDialog = (TextView) vi.findViewById(R.id.textViewDialogCheck);
        holder.cbDialog = (CheckBox) vi.findViewById(R.id.checkBoxDialogCheck);
        holder.cbDialog.setTag(position);
        vi.setTag(holder);
    } else
        holder = (ViewHolder) vi.getTag();

    holder.tvDialog.setText(withList.get(position).getName());
    holder.cbDialog.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            check[position] = isChecked;
        }
    });

    // This is the replacement of the for loop!
    holder.cbDialog.setChecked(checked[position]);

    return vi;
}

0

solved Checkbox is unchecked whenever listview is scrolled, android?