-5

I have a custom listview with BASEADAPTER I created a boolean for maintaining the state of the checkboxes. But everytime I scroll the listview the checkbox gets unchecked.

public class DialogListSearchAdapter extends BaseAdapter {
private ArrayList<POJOInventoryStore> withList;
private boolean[] check;

public DialogListSearchAdapter(ArrayList<POJOInventoryStore> withList) {
    this.withList = withList;
    check = new boolean[withList.size()];
    Arrays.fill(check, Boolean.FALSE);
}

@Override
public int getCount() {
    return withList.size();
}

@Override
public Object getItem(int position) {
    return withList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@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) {
            if (isChecked) {
                check[position] = Boolean.TRUE;
            } else {
                check[position] = Boolean.FALSE;
            }
        }
    });
    for (int i = 0; i < check.length; i++) {
        if (check[i]==Boolean.TRUE) {
            holder.cbDialog.setChecked(true);
        } else {
            holder.cbDialog.setChecked(false);
        }
    }

    return vi;
}

private static class ViewHolder {
    private TextView tvDialog;
    private CheckBox cbDialog;
}
}

What is wrong here? Am I missing something out?

WISHY
  • 11,067
  • 25
  • 105
  • 197

1 Answers1

1

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;
}
WarrenFaith
  • 57,492
  • 25
  • 134
  • 150