android - 如何从代码中检查自定义数组适配器中的复选框?
问题描述
我正在为 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;
}
解决方案
替换以下行:
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 和持有者项目。你会完成你的工作。
推荐阅读
- python - 如何在一排 pandas 数据框中找到多种数据?
- javascript - 如何在 JS 中对 3 个部分的对象数组进行排序?
- haskell - cabal 3.2 版:无法识别的命令:沙箱(尝试 --help)
- javascript - 使用 FileReader 将输入文件数组转换为 base64
- c++ - gdb 中没有加载符号?
- python - 诗歌上的 ImportError 但已安装模块
- fullcalendar - 在 Fullcalendar 中,有没有办法在不跨越的情况下每天单独显示全天事件?
- swift - 一次向 Firestore 添加大量文档(例如 1000 多个文档)时,应用程序挂起
- java - 执行jar时Java ArrayIndexOutofBounds错误
- javascript - 如何从 HTML 调用节点函数