1

Say, I'm having a ContentProvider (which in fact do not performs database call) and I want to pass some additional data (for example, call statistics) with the cursor to the caller:

public class SomeProvider extends ContentProvider {

    . . .      

    public Cursor query(....) {
        // I can not set extras for cursor here
        return new MyCursorImplementation(iterationData, callStats);
    }

}

In activity, I want to make:

Cursor cursor = getContentResolver().query(...);
CallStats callStats = ((MyCursorImplementation)cursor).getCallStats();

But I can't make this because cursor is already wrapped in ContentResolver.CursorWrapperInner and ClassCastException is thrown.

It'd be very handy when using AsyncTask:

protected class SomeAsyncTask extends AsyncTask<Uri, Void, Cursor> {


     ...


     @Override
     protected Cursor doInBackground(Uri... uris) {
         return getContentResolver().query(uris[0], ...);
     }

     @Override
     protected void onPostExecute(Cursor cursor) {
         if (cursor != null) {
             // update view with cursor data, do other things using cursor

             CallStats callStats = ((MyCursorImplementation)cursor).getCallStats();
             // do some UI changes using call statistics
             // ...but fails here
         } 
     }

}

How can I pass the data with the cursor or get exactly the same cursor that I've returned from query. Or it is impossible?

shaman.sir
  • 3,198
  • 3
  • 28
  • 36
  • 2
    Why aren't your "call stats" just additional columns in the `Cursor`? – CommonsWare Sep 08 '10 at 21:30
  • @CommonsWare Call stats are single object describing the state of several objects contained in the cursor. Say, the concrete type of objects that lie inside the cursor or the time in seconds query was performed - some single-query-related stats. – shaman.sir Sep 09 '10 at 06:35
  • 1
    So what? This is Java. Java has a single return value from methods. You are choosing to use the `ContentProvider` facade (for inexplicable reasons), and so the return value type is dictated by Android, not you. Hence, you need to fit your "call stats" into the `Cursor`. How, mechanically, you do that is up to you. – CommonsWare Sep 09 '10 at 11:03
  • @CommonsWare That's true, Thanks. I thought there is a mechanism similar to extras in Intents exists, without re-query. – shaman.sir Sep 09 '10 at 11:45

0 Answers0