2

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.

Community
  • 1
  • 1
Stefano
  • 23
  • 1
  • 4
  • I'm really confused about why its calling so many times on the same view (3 times for 47a30ec0 and 4 times for 47a30ec0)? Would think 2 times might be normal, but 3 times and then 4 times (maybe more in the future?) is really odd. – Bert F Feb 12 '11 at 15:30

1 Answers1

2
  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;
        }
    };
Bert F
  • 85,407
  • 12
  • 106
  • 123
  • Thanks Bert, I have edited my question, can you explain why it's firing so many times ? I am probably doing something wrong with the handling of the list :S – Stefano Feb 12 '11 at 13:57
  • @Stefano - seems like its getting called twice for 2 different views (2x2=4). Calling on 2 different views == seems like either there's 2 items in the list or the callback being used on two different lists. Calling twice on the same view == seems strange, but I don't know for sure that that's not the way it works - overall it shouldn't matter that its called twice on the same view. – Bert F Feb 12 '11 at 14:26
  • Ok, so this approach would work if I had a textbox AND a checkbox - using CheckedTextView I have 2 columns (NAME and CHECKED) for 1 single element, when I instantiate the SimpleCursorAdapter in the "FROM" I have TodoDbAdapter.NAME and in the "TO" R.id.checked_text - Then how can I get the checked state inside the ViewBinder ? Should I move the cursor manually somehow to the other column ? Thanks – Stefano Feb 12 '11 at 14:52
  • Scratch that, I solved it simply using Log.d("LIST","VIEW: "+view+" NAME: "+cursor.getString(columnIndex)+" "+cursor.getInt(checkedIndex)); :D It is a bit ugly, but seems to be working. Thanks. (editing the question now) – Stefano Feb 12 '11 at 15:01
  • @Stefano - "Should I move the cursor manually somehow to the other column?" You shouldn't think of a cursor moving across columns, it only moves across rows. Its fine to access a different/additional columns. I do this in the example above when I check for the latitude column, but the formatCoordinates uses lat and long. Similarly above, I look for the street column, but I format the entire address (street1, street2, city, state, zip). I wouldn't call it ugly. The alternative would be for simple adapter to allow mult-column -> view mapping which would be very complex. – Bert F Feb 12 '11 at 15:27
  • Ok, now the problem is that I am not able to select the checkbox O_O I tried to add inside the setViewValue: CheckedTextView item = (CheckedTextView) view; item.setChecked(true); item.setSelected(true); listView.setItemChecked(listView.indexOfChild(item), true); three different ways but the checkbox won't appear checked :| meh – Stefano Feb 12 '11 at 15:34
  • 1
    It seems the problem is listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); I'll just remove this and handle the clicks myself. Google should provide some more clear documentation on how to use (store/retrieve) this kind of lists, meh :) – Stefano Feb 12 '11 at 15:41