1

I have problems using CursorLoader. I need to retrieve phone and email adress for given name.

I implemented methods of LoaderManager.LoaderCallbacks<Cursor> interface. The problem is that I need to get contact_id from table contacts first, and then based on that contact_id I need to query RawContacts/Data table for email and phone.

I can't find example where you have more than one query in onCreateLoader(), usually you have return new CursorLoader(...). I have idea of using managedQuery(..) method to get id of contact and pass it to CursorLoader but I guess this isn't good idea. Any suggestion?

AndroidLearner
  • 4,500
  • 4
  • 31
  • 62
prowebphoneapp
  • 87
  • 1
  • 11
  • The question of this thread is your answer: http://stackoverflow.com/questions/7957418/loadermanager-with-multiple-loaders-how-to-get-the-right-cursorloader – macio.Jun Oct 30 '13 at 15:23

1 Answers1

2

In the OnCreateLoader, there is an id parameter which you can use to change the CursorLoader.

You can do something like

onCreateLoader(int id, bundle args) {

    switch(id) {

        case FETCH_CONTACT_ID: {
             //Return CursorLoader for fetching contactID
             break;
        }

        case FETCH_CONTACT_INFO: {
             //Return CursorLoader for fetching raw contacts
             break;
        }
   }
}

OnLoadFinished(Loader loader, Cursor c) {

    switch(loader.getId()) {

     case FETCH_CONTACT_ID: {
         //Contact IDs have been fetched, so start fetching raw contact data
         //Enter the IDs you want to fetch data for in the bundle which will be passed to onCreateLoader()
         mLoaderManager().restartLoader(FETCH_CONTACT_INFO, args);
         break;
    }

    case FETCH_CONTACT_INFO: {
         //Raw contact info has been fetched, do whatever you want with it
         break;
    }
    }
}
Vinay S Shenoy
  • 4,028
  • 3
  • 31
  • 36