我创建了一个ListActivity扩展arrayAdapter;列表中的每个元素都由2个标签和1个复选框组成。它工作得很好,除了当我取消选中其中一个列表框,我向上或向下滚动,使这个检查不可用;问题是,当我回到以前未选中的复选框时,它被选中了……我的listActivity有什么问题?这是一个基于arrayadapter的类:
private class DescrAdapter extends ArrayAdapter<StatDescriptor>{
private LayoutInflater inflater;
private final StatDescriptor[] descriptions;
private int layoutId;
public DescrAdapter(Context context, int res,StatDescriptor[] objects) {
super(context,res , objects);
this.inflater=LayoutInflater.from(context);
this.descriptions=objects;
this.layoutId=res;
}
public View getView(final int position, View rowView, ViewGroup parent) {
rowView = (RelativeLayout) inflater.inflate(layoutId, null);
TextView textName = (TextView) rowView.findViewById(R.id.StatName);
textName.setText(descriptions[position].getName());
TextView textDescr=(TextView) rowView.findViewById(R.id.StatDescr);
textDescr.setText(descriptions[position].getDescription());
CheckBox checkEnabled=(CheckBox) rowView.findViewById(R.id.checkStat);
checkEnabled.setChecked(descriptions[position].isEnabled());
checkEnabled.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
enabledStats[sortIndexes[position]]=!enabledStats[sortIndexes[position]];
}
});
return rowView;
}
}
谢谢你的帮助。
您使用descriptions[position]
来设置复选框的状态,但您翻转enabledStats[sortIndexes[position]]
。所以下次项目视图被重新创建与原始值,你没有改变(descriptions[position]
)。