我在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;
}
答案 0 :(得分:0)
替换以下行:
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。您将完成工作。