我的 ListActivity 有什么问题?
我创建了一个 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;
}
}
谢谢您的帮助。
I created a ListActivity extending an arrayAdapter; every element of the list is made up by 2 labels and 1 checkbox. It works perfectly fine, except when I uncheck one of those listbox and I scroll up or down, making this check unavailable; problem is that when I get back the previously uncheked checkbox it is checked...what's wrong with my listActivity? This is the ArrayAdapter-based class:
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;
}
}
Thank you for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用
descriptions[position]
来设置复选框的状态,但您翻转enabledStats[sortIndexes[position]]
。因此,下次项目视图将使用您未更改的原始值重新创建(descriptions[position]
)。You use
descriptions[position]
to set state of check box but you flipenabledStats[sortIndexes[position]]
. So next time item view gets recreated with original value that you didn't change (descriptions[position]
).