我的 ListActivity 有什么问题?

发布于 2024-12-09 06:59:57 字数 1499 浏览 7 评论 0原文

我创建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不顾 2024-12-16 06:59:57

您使用descriptions[position]来设置复选框的状态,但您翻转enabledStats[sortIndexes[position]]。因此,下次项目视图将使用您未更改的原始值重新创建(descriptions[position])。

You use descriptions[position] to set state of check box but you flip enabledStats[sortIndexes[position]]. So next time item view gets recreated with original value that you didn't change (descriptions[position]).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文