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

3.2k views Asked by At

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?

4

There are 4 answers

3
Gene On BEST ANSWER

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.

0
Dandre Allison On

If you want to use the Bundle in getExtras it seems that AbstractCursor which is extended by AbstractWindowedCursor which is extended by SQLiteCursor defines a setExtras method. It's documentation reads

Sets a android.os.Bundle that will be returned by getExtras(). null will be converted into android.os.Bundle.EMPTY.

respond is used for out-of-band communication with the Cursor according to the documentation.

So the answer is:

((AbstractCursor) cursor).setExtras(bundle);

Then you should be able to call

cursor.getExtras();

at a later time to retrieve that Bundle.

EDIT:

Upon looking further, it appears that setExtras is marked hidden for some reason, so it is public, and is intended to be used as described (and desired). See ContactsProvider2 example

So, I came up with using a CursorWrapper, and overriding the getExtras there to provide the Bundle. In my case, I create the Bundle in the CursorWrapper, so I don't need a method for setting it.

@Override
public Bundle getExtras() {
    return _meta_data;
}
0
rymo On

Here's an alternative (though you should not use it):

Class.forName("android.database.AbstractCursor")
        .getMethod("setExtras", new Class[]{Bundle.class})
        .invoke(c_in, extr);
1
Lalit Poptani On

I would insist you to have a look at MergeCursor and AbstractCursor to get around your problem and get the solution. For example you can check this answer.