Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 281
Mohamed_AbdAllah Avatar asked May 14 '26 02:05

Mohamed_AbdAllah


1 Answers

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;
}
like image 84
Dandre Allison Avatar answered May 15 '26 16:05

Dandre Allison



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!