本文介绍了如何转型,从managedQuery到LoaderManager / CursorLoader?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发的是针对API级别8(2.2,升级Froyo),Android应用程序。我使用的是的ContentProvider 键,这是很简单的,而我使用 SimpleCursorAdapter 填写我的列表视图,但我注意到了SimpleCursorAdapter该无支撑构造是pcated与下面的注释德$ P $:

Since I'm targeting API level 8, a LoaderManager isn't tied to an Activity. The FragmentActivity class in the compatibility package does this, but I'm not using Fragments.

My question is: how exactly should I be using LoaderManager/CursorLoader in an app targeting a pre-11 API level? Am I forced to transition to Fragments or should I just revert back to the deprecated SimpleCursorAdapter constructor (but make use of an AsyncTask to keep it UI thread friendly, which is what the CursorLoader is supposed to do)?

解决方案

Edit:

I've written fairly extensively about the LoaderManager in this blog post. Check it out and let me know if its helpful! :)


Original post:

Definitely, definitely, definitely go with LoaderManager. The CursorLoader class offloads the work of loading data on a thread, and keeps the data persistent during short term activity refresh events, such as an orientation change. In addition to performing the initial query, the CursorLoader registers a ContentObserver with the dataset you requested and calls forceLoad() on itself when the data set changes, and is thus auto-updating. This is extremely convenient as you don't have to worry about performing queries yourself. Of course it is possible to make use of AsyncTask to keep your application UI thread friendly, but it will involve a lot more code... and implementing your class so that it will, for example, retain the loaded Cursor over Activity won't be simple. The bottom line is that LoaderManager/Loader will do this automatically for you, as well as taking care of correctly creating and closing the Cursor based on the Activity lifecycle.

To use LoaderManager/CursorLoader in an app targeting a pre-11 API level, simply use the FragmentActivity class in the compatibility package. A FragmentActivity is just an Activity and has been created for Android compatibility support, and does not require the use of Fragments in your application. Just use getSupportLoaderManager() instead of getLoaderManager() and you should be all set. You could, of course, implement a parent FragmentActivity for each screen and have it displays a its layout in a Fragment (by making use of FragmentActivity.getSupportFragmentManager() in the Activity's onCreate() method). This design might make the transition to multi-pane layouts easier if you ever decide to optimize your application for tablets. It's a nice learning experience too :).

This is a pretty nice tutorial too. Try and work your way through it and don't hesitate to leave a comment if you have any other questions.

这篇关于如何转型,从managedQuery到LoaderManager / CursorLoader?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 03:32