gpt4 book ai didi

android - 如何从代码中检查自定义数组适配器中的复选框?

转载 作者:行者123 更新时间:2023-11-29 02:23:06 25 4
gpt4 key购买 nike

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

我可以获得 mCheckStates 数组并更改值,但它不会影响设计 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;

}

最佳答案

替换以下行:

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,但您需要为每一行中的每个复选框实现它。为每行中的每个复选框实现带有 holder 项的新 onCheckChangelistener。你会完成你的工作。

关于android - 如何从代码中检查自定义数组适配器中的复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53986108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com