【问题标题】:How to check checkbox in an custom array adapter from code?如何从代码中检查自定义数组适配器中的复选框?
【发布时间】:2019-05-27 21:45:59
【问题描述】:

我正在为 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;

}

【问题讨论】:

  • 你能详细说明你面临的具体问题吗
  • 我想从主活动中更改复选框状态,我有一个带有自定义适配器的列表视图。我可以获取复选框并将它们的状态更改为 false,但它没有显示在 Android 设备上,它们仍然处于选中状态!

标签: android custom-arrayadapter


【解决方案1】:

替换以下行:

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 和持有者项目。你会完成你的工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 1970-01-01
    • 2015-01-25
    • 1970-01-01
    相关资源
    最近更新 更多