1

I'd like to populate a listview from 2 tables. Anyone know how I can achieve this? Currently what I have looks like but it only works with one adapter:

   private void populate() {
        todoCursor = dbNotes.getTodoKey();
        startManagingCursor(todoCursor);
        todo = 
             new SimpleCursorAdapter(
               this,
               R.layout.todo_list, 
               todoCursor,
               new String[] {databaseHelper.DB_COLUMN_TODO_KEYS},
               new int[] {R.id.textTodo});
        setListAdapter(todo);
    }

    private void fillData(int i) {
        Cursor notesCursor = dbNotes.retrieveAll(i, "=0");
        startManagingCursor(notesCursor);
        notes = 
             new SimpleCursorAdapter(
               this,
               R.layout.list_item, 
               notesCursor,
               new String[] {databaseHelper.DB_COLUMN_SUBJECT, databaseHelper.DB_COLUMN_TIME, databaseHelper.DB_COLUMN_MESSAGE, databaseHelper.DB_COLUMN_DOW, databaseHelper.DB_COLUMN_MD},
               new int[] {R.id.text1, R.id.text2, R.id.text3, R.id.textDay, R.id.textDayOfWeek});
        setListAdapter(notes);
    }
Romain Hippeau
  • 24,113
  • 5
  • 60
  • 79
Paul
  • 1,714
  • 7
  • 22
  • 45
  • you could create a view that joins the tables and query that instead, like it's done in Android's ContactsProvider http://androidxref.com/4.4.4_r1/xref/packages/providers/ContactsProvider/src/com/android/providers/contacts/ContactsDatabaseHelper.java#Views – faizal Aug 21 '14 at 13:18

1 Answers1

3

I'd like to populate a listview from 2 tables. Anyone know how I can achieve this?

You haven't exactly explained what you mean by "populate a listview from 2 tables".

So, depending on your definition, you could:

  • Use a join to create a single result set from both tables
  • Use my MergeCursor to concatenate adapters on your two result sets into a single adapter
  • Assemble a single MatrixCursor from your disparate parts that you then put into the list

There are probably other solutions as well.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491