CursorAdapter + CheckBox,如果检查某个项目,还会检查其他项目

时间:2012-09-12 20:28:58

标签: android adapter simplecursoradapter android-cursoradapter

我尝试为ListView创建自己的适配器,每行都有复选框。一切正常,但问题是,当我检查一些复选框然后向下滚动时,其他一个也被检查。

(当我第一次检查时,第11次,第21次,......也被检查)我可以解释一下这个问题吗?

谢谢。

我的适配器:

public class WordAdapter extends CursorAdapter {

    public WordAdapter(Context context, Cursor cursor, int flags){
        super(context, cursor, flags);
    }

    @Override
    public void bindView(View oldView, Context ctx, Cursor c) {

        int wordQuestionIndex = c.getColumnIndex( WordDBAdapter.QUESTION );
        int wordAnswerIndex = c.getColumnIndex( WordDBAdapter.ANSWER );
        int activeIndex = c.getInt(c.getColumnIndex( WordDBAdapter.ACTIVE ));
        String question = c.getString(wordQuestionIndex);
        String answer = c.getString(wordAnswerIndex);
        int active = c.getInt(activeIndex);

        Log.d("WordAdapter", "isActive: "+ active + " - " + question + ", " + answer);

        ((TextView) oldView.findViewById(R.id.adapter_question)).setText(question);
        ((TextView) oldView.findViewById(R.id.adapter_answer)).setText(answer);
        CheckBox checkBox =(CheckBox) oldView.findViewById(R.id.myCheckBox);
        //checkBox.setChecked(vh.isChecked);
    }


    @Override
    public View newView(Context ctx, Cursor c, ViewGroup root) {
        LayoutInflater inflater = LayoutInflater.from(ctx);
        View view = inflater.inflate(R.layout.word, root, false);
        return view;
    }   
}

1 个答案:

答案 0 :(得分:1)

这是因为旧视图的重用......

我建议你保留一个勾选复选框的项目列表,并在每次初始化新值时重置bindview中的复选框项。

类似的问题和解决方案是found here