我正在开发一个针对 API 级别 8(2.2,Froyo)的 Android 应用程序。我正在使用 ContentProvider
这很简单,我正在使用 SimpleCursorAdapter
来填写我的列表视图,但我在 SimpleCursorAdapter 无标志构造函数是已弃用,并带有以下注释:
此构造函数已弃用。
不鼓励使用此选项,因为它会导致在应用程序的 UI 线程上执行游标查询,从而导致响应能力差,甚至导致应用程序无响应错误。作为替代方案,将 LoaderManager 与 CursorLoader 一起使用。
由于我的目标是 API 级别 8,因此 LoaderManager
未绑定到 Activity
。兼容性包中的 FragmentActivity
类可以执行此操作,但我没有使用 Fragments。
我的问题是:我应该如何在针对 11 之前的 API 级别的应用中使用 LoaderManager/CursorLoader
?我是否被迫过渡到 Fragments 还是应该恢复到已弃用的 SimpleCursorAdapter 构造函数(但使用 AsyncTask 来保持 UI 线程友好,这就是CursorLoader 应该做什么)?
I'm developing an Android application that is targeting API level 8 (2.2, Froyo). I'm using a ContentProvider
and that's simple enough, and I'm using SimpleCursorAdapter
to fill out my list view, but I noticed in the documentation for SimpleCursorAdapter that the flagless constructor is deprecated with the following note:
This constructor is deprecated.
This option is discouraged, as it results in Cursor queries being performed on the application's UI thread and thus can cause poor responsiveness or even Application Not Responding errors. As an alternative, use LoaderManager with a CursorLoader.
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)?
发布评论
评论(1)
编辑:
我在这个 LoaderManager 的文章>博客文章。检查一下并告诉我是否有帮助! :)
原帖:
绝对,绝对,绝对使用
LoaderManager
。 CursorLoader 类卸载了在线程上加载数据的工作,并在短期活动刷新事件(例如方向更改)期间保持数据持久性。除了执行初始查询之外,CursorLoader
还会向您请求的数据集注册一个ContentObserver
,并在数据集出现时调用自身的forceLoad()
变化,因此会自动更新。这非常方便,因为您不必担心自己执行查询。当然,可以使用AsyncTask
来保持应用程序 UI 线程友好,但它将涉及更多代码......并实现您的类,以便它能够保留通过Activity
加载Cursor
并不简单。最重要的是,LoaderManager/Loader
会自动为您执行此操作,并根据Activity
正确创建和关闭Cursor
代码>生命周期。要在针对 11 之前的 API 级别的应用中使用
LoaderManager/CursorLoader
,只需使用兼容性包中的FragmentActivity
类即可。FragmentActivity
只是一个Activity
,是为了 Android 兼容性支持而创建的,不需要在应用程序中使用Fragment
。只需使用getSupportLoaderManager()
而不是getLoaderManager()
就可以了。当然,您可以为每个屏幕实现一个父FragmentActivity
并让它在Fragment
中显示其布局(通过使用FragmentActivity.getSupportFragmentManager()
在 Activity 的onCreate()
方法中)。如果您决定针对平板电脑优化应用程序,这种设计可能会使向多窗格布局的过渡变得更容易。这也是一次很好的学习经历:)。这也是一个非常好的教程 。尝试并解决它,如果您有任何其他问题,请随时发表评论。
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
. TheCursorLoader
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, theCursorLoader
registers aContentObserver
with the dataset you requested and callsforceLoad()
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 ofAsyncTask
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 loadedCursor
overActivity
won't be simple. The bottom line is thatLoaderManager/Loader
will do this automatically for you, as well as taking care of correctly creating and closing theCursor
based on theActivity
lifecycle.To use
LoaderManager/CursorLoader
in an app targeting a pre-11 API level, simply use theFragmentActivity
class in the compatibility package. AFragmentActivity
is just anActivity
and has been created for Android compatibility support, and does not require the use ofFragment
s in your application. Just usegetSupportLoaderManager()
instead ofgetLoaderManager()
and you should be all set. You could, of course, implement a parentFragmentActivity
for each screen and have it displays a its layout in aFragment
(by making use ofFragmentActivity.getSupportFragmentManager()
in the Activity'sonCreate()
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.