Using Android CursorLoaders and other Honeycomb features on a phone app (Froyo)
I've been working on a phone app using Froyo (my phone version). I would like to switch to include Honeycomb in the project.
I've imported the Android Compatibility Package into my project. My current app reads from a SQLite database. Loads a Gallery and various GridViews and ListViews from said database. I don't know what to do next. Do I switch Activities to FragmentActivities? How do I go from my current Cursors to CursorLoaders? How does that affect the Custom Adapters I have for loading the gallery and grid/list views? Etc...
I'd appreciate any help and advice you can give. I'm having trouble just grasping how to convert what I have and to allow both to coexist. I really want to do it "the right way".
EDIT: I realize this is a rather broad "question". So, let's concentrate on the Cursor to CursorLoader thing. I'll create new questions for the other i开发者_JS百科tems.
With compatibility library I've used CursorLoader and Fragments targeting 2.1.
Loaders are pretty easy if you have a ContentProvider backing them, Fragments require the use of FragmentActivity (maybe LoaderCursors too).
Loaders do require however the LoaderManager.LoaderCallbacks interface.
Loaders have a life-cycle, I don't have code at hand but it's like this.
LoaderManager.InitLoader(USER_SPECIFIED_ID_OF_LOADER);
---- Which eventually call --->
Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(getActivity(), baseUri,
CONTACTS_SUMMARY_PROJECTION, select, null,
Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
}
This is where your Loader does its loader thing, I only used CursorLoader in my code so I don't know more about the gritty details.
When the loader is finished, the final callback is invoked.
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
//Do whatever you want with your cursor here.
}
That said, they work pretty well and do their job as expected, two thumbs up.
*Edit: There is also a onLoaderRestarted() that probably gets called when you restart your loader, not sure exactly when it's called.
Are you wanting your app to run on both Froyo and Honeycomb? Or switch to Honeycomb only?
If you're only interested in running on Honeycomb devices, you simply need to replace all of your deprecated method calls (Activity.managedQuery()
) with their Honeycomb replacements (CursorLoader
).
If you want to run on both versions of Android (or earlier), then you can use reflection. Basically reflection tests if a particular method or class (for example, CursorLoader) is available on the current system, and allows you to decide what to do in each case. Here is a tutorial regarding reflection: http://mobile.tutsplus.com/tutorials/android/java-reflection/
OR, you can simply use the deprecated methods as normal and they should run properly on Honeycomb. Check here for further information about maximizing app compatibility with Honeycomb, including making sure the app fills the screen, and allowing use of the Action bar.
精彩评论