2

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 items.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Metallicraft
  • 2,211
  • 5
  • 31
  • 52

2 Answers2

5

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.

HaMMeReD
  • 2,440
  • 22
  • 29
  • I'm using a SQLite database in the app, what do I use for the URI in the loader? Or better yet, is there an example of using a CursorLoader with a SQLite db that you're aware of? – Metallicraft Jul 11 '11 at 00:12
  • 1
    The best answer I can give you is that you need to implement a ContentProvider to interface the SQLite db to use the CursorLoader with a uri. Motodev studio provides code-generation facilities for sql-lite db's and turning them into database interface classes & content providers, haven't used it yet but it's worth looking at if you have a db and want to make content providers quickly. – HaMMeReD Jul 11 '11 at 07:28
-1

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.

howettl
  • 12,419
  • 13
  • 56
  • 91
  • I do want to make it compatible for phones and tablets. I know cursors "can" work on honeycomb, but from what I've read it's recommended to move to Loaders and LoaderManagers while using the compatibility package (as expressed [here](http://stackoverflow.com/questions/5603504/android-3-0-what-are-the-advantages-of-using-loadermanager-instances-exactly)). I just don't know how to go about switching. It seems that loaders are only available for Fragments?? – Metallicraft Jul 10 '11 at 23:50
  • I'm currently developing an app which works on Honeycomb and below (including Action bar support). I use cursors for database calls, and while they are prone to errors, they are generally avoidable. If you want to use Loaders then you will need to use reflection. Where did you read they are only available to fragments? According to the [docs](http://developer.android.com/reference/android/app/LoaderManager.html) they are available to Activities as well. – howettl Jul 11 '11 at 00:03
  • I'm not sure that I read that they were only available to Fragments, but when I tried to replace my cursors with loaders, I had errors until I changed my activity to a fragmentactivity. I could have been doing something totally wrong though. – Metallicraft Jul 11 '11 at 00:09
  • 2
    using java reflection to see if a current class is available is a bad idea, it's the job of the compiler to determine if a library is linkable or not. You need the [Compatibility Library](http://developer.android.com/sdk/compatibility-library.html) to get he CursorLoader/Fragments working in <3.0. Here's a tutorial that targets >1.5 http://helpmeco.de/2012/3/using-an-android-cursor-loader-with-a-content-provider – browep Mar 19 '12 at 20:40
  • To expand on what @browep said, you shouldn't be using reflection to check if methods are available. Instead, check the Android version with `Build.VERSION.SDK_INT` to determine what's available. As far as Fragments go, the compatibility libraries cover all the necessary classes to implement Loaders. Your `Activity` will need to be changed to `FragmentActivity`. Don't worry though, because while a `FragmentActivity` is necessary in this case to use Loaders and Fragments, you don't need to switch to Fragments in order to use Loaders. You can mostly just treat the two Activities the same way. – mkuech Jan 30 '13 at 23:55