【问题标题】:CursorAdapter + CheckBox, if check some item, some other is checked tooCursorAdapter + CheckBox,如果选中某项,则也选中其他项
【发布时间】:2012-09-05 22:05:43
【问题描述】:

我尝试为 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;
    }   
}

【问题讨论】:

标签: android adapter simplecursoradapter android-cursoradapter


【解决方案1】:

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

我建议,您维护一个复选框项目列表,每次使用新值初始化时都会在 bindview 中勾选并重置复选框项目.​​.

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-07
    • 2018-03-20
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多