开发者

What's wrong with my ListActivity?

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 Des开发者_运维技巧crAdapter(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.


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]).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜