Gurunandan Bhat
Gurunandan Bhat

Reputation: 3562

Is it possible to use CursorLoader in an Activity not Fragment with android compatibility

Can I use a CursorLoader in a subclass of an Activity (not a subclass of FragmentActivity) with the android-compatibility library? If I can, how do I get the Cursorloader since getLoaderManager().init(...) is not available in an activity subclass with the compatibility layer.

Upvotes: 0

Views: 1706

Answers (2)

Nikolay Elenkov
Nikolay Elenkov

Reputation: 52936

Not unless you are using Honeycomb and above. FragmentActivity includes the code necessary to manage loaders, pre-Honeycomb Activity doesn't.

Why don't you want to extend FragmentActivity?

Upvotes: 5

Necronet
Necronet

Reputation: 6813

From the official documentation:

A loader that queries the ContentResolver and returns a Cursor. This class implements the Loader protocol in a standard way for querying cursors, building on AsyncTaskLoader to perform the cursor query on a background thread so that it does not block the application's UI.

The CursorLoadeer class just query a ContentResolver returning a cursor, so your activity class must implement LoaderCallbacks override the method and return the CursorLoader on the onCreateLoader()

  public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
                + Contacts.HAS_PHONE_NUMBER + "=1) AND ("
                + Contacts.DISPLAY_NAME + " != '' ))";

        return new CursorLoader(getActivity(), Contacts.CONTENT_URI,
                CONTACTS_SUMMARY_PROJECTION, select, null,
                Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
    }

I don't know that getCursorLoader().init(...) you're talking about if yo are refering to the getLoaderManager() it is available on the Activity Class since API 11

Upvotes: 0

Related Questions