首页 > 解决方案 > 如何从代码中检查自定义数组适配器中的复选框?

问题描述

我正在为 android studio 中的列表视图使用自定义适配器。每行都有一个复选框和一个文本视图。我可以从此自定义数组适配器中获取选中的项目,但我无法从代码中更改复选框的状态。我需要取消选中复选框。这是自定义数组适配器代码:

我可以获得 mCheckStates 数组并更改值,但它不会影响设计视图中的复选框状态。

  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;

}

标签: androidcustom-arrayadapter

解决方案


替换以下行:

holder.chkSelect.setOnCheckedChangeListener(this);

和:

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
        }

并从您的代码中删除以下行:

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

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

}

您正在声明通用 onChangelistener 但您需要为每一行中的每个复选框实现它。为每行中的每个复选框实现新的 onCheckChangelistener 和持有者项目。你会完成你的工作。


推荐阅读