androidsqlitecursorandroid-viewbindermergecursor

Using cursor.respond(Bundle) & cursor.getextras()


I am having 2 cursors from different tables in an SQLite database. I am trying to put the data from the two cursors into one ListView but for different formatting for data from each cursor.

What I thought about is using a MergeCursor to combine both cursors, but the ViewBinder for my SimpleCursorAdapter will see them as a single cursor and will not be able to differentiate for formatting (unless I alter my tables, which I do not want to do).

Finally, I found 2 methods called Cursor.repond(Bundle) & Cursor.getExtras(), but the documentation on the developer console is very short and Googling these methods did not clarify their use.

I tested my idea to use resond() in my Database class for the query:

    extr.putString("table", the_tab);
    Cursor c_in = db.rawQuery(qry, null);
    c_in.respond(extr);
    return c_in;

And use getExtras() in the ViewBinder to know the table of the query and format the ListView item accordingly:

Bundle extr=cur.getExtras();
String tab= extr.getString("table");

But I am always getting an exception that tab is null.

My question after this long description is: Am I using the respond and getExtras methods correctly? And if not, is there a better approach for my problem?


Solution

  • Try building the table name in as a field in the two SELECTs.

    SELECT "A", * from tableA;  SELECT "B", * from tableB;
    

    then compute a merged cursor. Alternately,

    SELECT "A", * from tableA UNION ALL SELECT "B", * from tableB;
    

    Now each row of the cursor will have a "A" in the first column if it came from tableA and a "B" if it came from tableB. So it's easy to look at this column in your ViewBinder to make formatting decisions.