Search code examples
androidandroid-listviewsimplecursoradaptercheckedtextviewandroid-viewbinder

CheckedTextView load default values from SimpleCursorAdapter


I have an adapter that uses a CheckedTextView and extends from a SimpleCursorAdapter. I tried to load the default values for CheckTextView but I didn't manage to do it. I don't know if this it's the behavior of CheckedTextView or I am doing something wrong.

What I have tried?

I tried to override the getView() method and get the CheckedTextView view and use setChecked(boolean), FAILED, weird that it doesn't work.

Then I tried to use a ViewBinder, but that doesn't work either.

Any insight to this problem?

adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
            public boolean setViewValue(View view, Cursor cursor,
                    int columnIndex) {
                CheckedTextView cb = (CheckedTextView) view;
                int checked=cursor.getInt(cursor.getColumnIndex(ProgramSurvey.CHECKED));
                cb.setChecked(checked==1);

                return false;
            }
        });

Solution

  • If the View returned from getView is a CheckedTextView (or any other view that implements Checkable), then the listView will set the checked on the relevant rows, based on which rows it thinks are selected (from calls to setItemChecked). You'll need to tell the ListView which rows are checked, rather than trying to set it yourself, or alternatively wrap your view in a view which doesn't implement checkable, then you'll be able to set checked yourself in the CheckedTextView.