Android: ListView of CheckedTextView + SQLite, display checked rows with Adapter?

3.5k views Asked by At

I am trying to implement a very simple "todo" list. I chose to use a ListView of CheckedTextView (with id checked_text), the ListView uses CHOICE_MODE_MULTIPLE of course.

Now, I am using the SQLite database to store the list items.

To fill the list I have something like:

cursor = dbHelper.fetchAll();
startManagingCursor(cursor);

String[] from = new String[] {  DbAdapter.NAME };
int[] to = new int[] { R.id.checked_text };

SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
R.layout.todo_multi_row, cursor, from, to);
listView.setAdapter(notes);

This is working fine, it gets all the items in the DB and for each item it creates a CheckedTextView with as text DbAdapter.NAME.

But what I really need is to get both the text, and the checked state, and check the Rows accordingly.

Let's say I can get the state (as integer 0/1) from the DB using DbAdapter.CHECKED.. how can I implement a cursor adapter that gets the job done?

I searched everywhere (for instance this question which is a mess) but found nothing to solve this problem. Wah. It seemed so easy at first.

Thanks,
Stefano


@Bert F:
Thanks, this is the method I am using to fill my list:

private void fillData() {
        Log.d("LIST","fill");
        cursor = dbHelper.fetchAllTodos();
        startManagingCursor(cursor);

        String[] from = new String[] { TodoDbAdapter.NAME };
        int[] to = new int[] { R.id.checked_text };

        SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
                R.layout.todo_multi_row, cursor, from, to);

        final SimpleCursorAdapter.ViewBinder mViewBinder =
            new SimpleCursorAdapter.ViewBinder() {
                @Override
                public boolean setViewValue(
                        final View view,
                        final Cursor cursor,
                        final int columnIndex) {

                    Log.d("LIST",view +" "+cursor+" "+columnIndex);

                    CheckedTextView item = (CheckedTextView) view;
                    Log.d("LIST","NAME: "+item.getText()+" State: "+item.isChecked());

                    return false;
                }
            };

        notes.setViewBinder(mViewBinder);
        listView.setAdapter(notes);
    }

Now, as a test I am just printing out what is happening.
If I add 1 element to the list what I see is:

02-12 14:45:35.913: DEBUG/LIST(22621): fill
02-12 14:45:35.933: DEBUG/LIST(22621): android.widget.CheckedTextView@479da398 android.database.sqlite.SQLiteCursor@479d98f0 2
02-12 14:45:35.933: DEBUG/LIST(22621): NAME:  State: false
02-12 14:45:35.933: DEBUG/LIST(22621): android.widget.CheckedTextView@479da398 android.database.sqlite.SQLiteCursor@479d98f0 2
02-12 14:45:35.943: DEBUG/LIST(22621): NAME: first item State: false
02-12 14:45:36.013: DEBUG/LIST(22621): android.widget.CheckedTextView@479dc6c0 android.database.sqlite.SQLiteCursor@479d98f0 2
02-12 14:45:36.013: DEBUG/LIST(22621): NAME:  State: false
02-12 14:45:36.223: DEBUG/LIST(22621): android.widget.CheckedTextView@479dc6c0 android.database.sqlite.SQLiteCursor@479d98f0 2
02-12 14:45:36.223: DEBUG/LIST(22621): NAME: first item State: false

And if I check it..

02-12 14:53:33.123: DEBUG/LIST(22621): android.widget.CheckedTextView@479dc6c0 android.database.sqlite.SQLiteCursor@479d98f0 2
02-12 14:53:33.123: DEBUG/LIST(22621): NAME: first item State: false
02-12 14:53:33.123: DEBUG/LIST(22621): android.widget.CheckedTextView@479da398 android.database.sqlite.SQLiteCursor@479d98f0 2
02-12 14:53:33.123: DEBUG/LIST(22621): NAME: first item State: false

Let aside the fact that it says it is not checked, why is it firing so many times ? I just have 1 item :I


EDIT: ugly solution

private void fillData() {
        Log.d("LIST","fill");
        cursor = dbHelper.fetchAllTodos();
        startManagingCursor(cursor);

        String[] from = new String[] { TodoDbAdapter.NAME };
        int[] to = new int[] { R.id.checked_text };

        SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
                R.layout.todo_multi_row, cursor, from, to);

        final SimpleCursorAdapter.ViewBinder mViewBinder =
            new SimpleCursorAdapter.ViewBinder() {
                @Override
                public boolean setViewValue(
                        final View view,
                        final Cursor cursor,
                        final int columnIndex) {

                     final int checkedIndex =
                         cursor.getColumnIndexOrThrow(
                                 TodoDbAdapter.CHECKED);

                     Log.d("LIST","VIEW: "+view+" NAME: "+cursor.getString(columnIndex)+" "+cursor.getInt(checkedIndex));

                    return false;
                }
            };

        notes.setViewBinder(mViewBinder);
        listView.setAdapter(notes);
    }

With 2 entry (first selected second not) when I load the list I have:

02-12 15:59:48.613: DEBUG/LIST(23533): fill
02-12 15:59:48.643: DEBUG/LIST(23533): VIEW: android.widget.CheckedTextView@47a30ec0 NAME: test 1
02-12 15:59:48.653: DEBUG/LIST(23533): VIEW: android.widget.CheckedTextView@47a30ec0 NAME: LOL 0
02-12 15:59:48.683: DEBUG/LIST(23533): VIEW: android.widget.CheckedTextView@47a30ec0 NAME: test 1
02-12 15:59:48.683: DEBUG/LIST(23533): VIEW: android.widget.CheckedTextView@47a331f8 NAME: LOL 0
02-12 15:59:48.713: DEBUG/LIST(23533): VIEW: android.widget.CheckedTextView@47a35770 NAME: test 1
02-12 15:59:48.713: DEBUG/LIST(23533): VIEW: android.widget.CheckedTextView@47a35770 NAME: LOL 0
02-12 15:59:48.783: DEBUG/LIST(23533): VIEW: android.widget.CheckedTextView@47a35770 NAME: test 1
02-12 15:59:48.783: DEBUG/LIST(23533): VIEW: android.widget.CheckedTextView@47a35770 NAME: LOL 0

Cheers.

1

There are 1 answers

7
Bert F On BEST ANSWER
  1. Implement a SimpleCursorAdapter.ViewBinder.

  2. Have the ViewBinder.setViewValue() method check for when its called for the DbAdapter.NAME column or else have it check for the R.id.checked_text-type view.

  3. When it called, but not for not the target column/view, have it return false so that the adapter will do bind the column/view the way it normally would.

  4. When it called for the target column/view, have it set the text and checkbox for the (CheckedTextView) view. It should end up returning true so that adapter won't continue to attempt to bind the view itself. Note that the cursor is available to access query data to determine whether to check the checkbox or not (DbAdapter.CHECKED).

  5. Set your ViewBinder in your SimpleCursorAdapter via setViewBinder()

Here's one of my ViewBinder implementations. Its not for checboxes, rather its for doing some fancy formatting of a text view, but it should give you some idea for the approach:

private final SimpleCursorAdapter.ViewBinder mViewBinder =
    new SimpleCursorAdapter.ViewBinder() {
        @Override
        public boolean setViewValue(
                final View view,
                final Cursor cursor,
                final int columnIndex) {
            final int latitudeColumnIndex =
                cursor.getColumnIndexOrThrow(
                        LocationDbAdapter.KEY_LATITUDE);
            final int addressStreet1ColumnIndex =
                cursor.getColumnIndexOrThrow(
                        LocationDbAdapter.KEY_ADDRESS_STREET1);

            if (columnIndex == latitudeColumnIndex) {

                final String text = formatCoordinates(cursor);
                ((TextView) view).setText(text);
                return true;

            } else if (columnIndex == addressStreet1ColumnIndex) {

                final String text = formatAddress(cursor);
                ((TextView) view).setText(text);
                return true;

            }

            return false;
        }
    };