1

I try create my own Adapter for ListView with checkboxes on each row. All works fine, but problem is, when I check some checkbox and then scroll down, other one is checked too.

(When I check first, 11th, 21th, ..., is checked too) Can me anyone explain wher is the problem?

Thank you.

My adapter:

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;
    }   
}
Vinay
  • 2,395
  • 3
  • 30
  • 35
Peter Jurkovic
  • 2,686
  • 6
  • 36
  • 55
  • possible duplicate of [Android: CursorAdapter, ListView and CheckBox](http://stackoverflow.com/questions/4803756/android-cursoradapter-listview-and-checkbox) – Sam Sep 12 '12 at 20:50

1 Answers1

1

This is because of the reuse of the old view...

I would suggest, you maintain a list of check boxes items which are ticked and reset the check box item in bindview every time you initialize with new values..

Similar Question and solution is found here

Community
  • 1
  • 1
Vinay
  • 2,395
  • 3
  • 30
  • 35