Java – ListAdapter adds only one type of data to a ListView

ListAdapter adds only one type of data to a ListView… here is a solution to the problem.

ListAdapter adds only one type of data to a ListView

Can someone help me with this? I have two cursor items.

  • One gets time data
  • Another is to get the name of the task from the database.

But it just sets the task in the ListView. When I delete the task cursor, it sets the time, but not both.

I want this list: Time – Task Name

Here is the code part of it:

public void updateUI() {
    helper = new TaskDBHelper(MainActivityPhone.this);
    SQLiteDatabase sqlDB = helper.getReadableDatabase();

Cursor cursor = sqlDB.query(TaskContract.TABLE,
            new String[]{TaskContract.Columns._ID, TaskContract.Columns.TIME},
            null, null, null, null, TaskContract.Columns.TIME + " ASC");

Cursor cursor2 = sqlDB.query(TaskContract.TABLE,
            new String[]{TaskContract.Columns._ID, TaskContract.Columns.TASK},
            null, null, null, null, TaskContract.Columns.TIME + " ASC");

listAdapter = new SimpleCursorAdapter(
            this, R.layout.task_view,cursor,
            new String[]{TaskContract.Columns.TIME, TaskContract.Columns.TASK},
            new int[]{R.id.timeTextView, R.id.taskTextView},
            0
    );

  Log.v("Cursor Object", DatabaseUtils.dumpCursorToString(cursor));

/*   Cursor cursor2 = sqlDB.query(TaskContract.TABLE,
            new String[]{TaskContract.Columns._ID, TaskContract.Columns.TASK},
            null, null, null, null, TaskContract.Columns.TIME + " ASC");

listAdapter = new SimpleCursorAdapter(
            this,
            R.layout.task_view,
            cursor2,
            new String[]{TaskContract.Columns.TASK},
            new int[]{R.id.taskTextView},
            0
    ); */

ListView listView = (ListView) findViewById(R.id.list);
    listView.setAdapter(listAdapter);

}

Solution

A few things you can do:

  • Create your own cursor adapter so you can pass 2 cursors and display the data as needed.

  • Combine cursors with the data you need so you can use standard adapters.

  • Combine cursors into an array of strings. So you’ll get the data you need and save it in a string like this: Time – Task Name. Then populate the ListView with an ArrayAdapter.

If you google it, I think there is enough code for all 3 of these methods. I’m assuming the problem is that the current adapter doesn’t have the logic to combine two tables.

Related Problems and Solutions