2

I am using a custom adapter for a listview in the android studio. There are a checkbox and a text view in every row. I can get checked items from this custom array adapter but I can't change the state of checkbox from code. I need to uncheck the checkboxes. Here is the custom array adapter code:

I can get the mCheckStates array and change the value but it isn't affecting the checkbox state in the design view.

  public class CustomAdapter extends ArrayAdapter<LstItem> implements CompoundButton.OnCheckedChangeListener
{  public SparseBooleanArray mCheckStates;

Context context;
int layoutResourceId;
List<LstItem>  data = null;

public CustomAdapter(Context context, int layoutResourceId, List<LstItem> data){
    super(context, layoutResourceId,data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
    mCheckStates = new SparseBooleanArray(data.size());
}

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


    View row = convertView;
    ItemHolder holder= null;

    if (row == null){

        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new ItemHolder();

        holder.txtVisValue = (TextView) row.findViewById(R.id.TxtVisValue);
        holder.txtInvisValue = (TextView) row.findViewById(R.id.TxtInvisValue);
        holder.chkSelect = (CheckBox) row.findViewById(R.id.Chk);
        Log.d("test", "rownull:"+holder.txtVisValue.getText());

        row.setTag(holder);

    }
    else
        {

        holder = (ItemHolder)row.getTag();
            Log.d("test", "rownotnull:"+ holder.txtVisValue.getText());
    }


    LstItem item = data.get(position);
    holder.txtVisValue.setText(item.visValue);
    holder.txtInvisValue.setText(item.invisibleValue);

    // holder.chkSelect.setChecked(true);
    holder.chkSelect.setTag(position);
    holder.chkSelect.setChecked(mCheckStates.get(position, false));
    holder.chkSelect.setOnCheckedChangeListener(this);
    return row;

}


public boolean isChecked(int position) {
    return mCheckStates.get(position, false);
}

public void setChecked(int position, boolean isChecked) {
    mCheckStates.put(position, isChecked);

}

public void setCheckedchk(int position, boolean isChecked) {
    mCheckStates.put(position, isChecked);

}

public void toggle(int position) {
    setChecked(position, !isChecked(position));

}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    mCheckStates.put((Integer) buttonView.getTag(), isChecked);
    Log.d("test", "chechchange:"+ String.valueOf(buttonView.isChecked()));

}
static class ItemHolder
{
    TextView txtVisValue;
    TextView txtInvisValue;
    CheckBox chkSelect;

}

1 Answers1

0

Replace following line:

holder.chkSelect.setOnCheckedChangeListener(this);

with:

holder.chkSelect.setOnCheckedChangeListener(new OnCheckedChangeListener(){

    @Override
    public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) 
    {
        if (isChecked)
        {
            // do your work here in case checkbox is checked
        }
        else if (!isChecked)
        {
            // do your work here in case checkbox is not checked
        }

And remove following lines from your code:

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

mCheckStates.put((Integer) buttonView.getTag(), isChecked);
Log.d("test", "chechchange:"+ String.valueOf(buttonView.isChecked()));

}

You are declaring generic onChangelistener but you need to implement that for every single checkbox in every row. Implement that new onCheckChangelistener with holder item for every checkbox in each row. You will get your job done.

Muhammad Saad Rafique
  • 3,158
  • 1
  • 13
  • 21