SimpleCursorAdapter 的旧构造函数已弃用..真的吗?
这里 它说SimpleCursorAdapter
的 API 级别 1 构造函数已弃用,建议使用 LoaderManager
和 CursorLoader
。
但深入研究 LoaderManager
和 CursorLoader
的使用我发现 this 示例位于扩展 ListFragment
的内部类中(我想是 Fragment 本身的扩展)我们创建一个 CursorLoader。一切看起来都很好,除了 CursorLoader
将 Uri
作为参数。因此,这意味着我需要创建一个 ContentProvider 来访问我的数据库。
我必须承认,仅仅为了创建一个包含来自数据库的项目的简单 ListView
就必须完成所有这些工作,这看起来有点过分了。特别是如果我无意将我的数据库数据提供给其他应用程序,而内容提供商的主要目的就是这样做。
那么这真的值得吗?
特别是在像我这样的情况下,要获取的内容可能很小。我正在认真考虑以旧方式做这件事,你觉得怎么样?
Here it says that SimpleCursorAdapter
's API level 1 constructor is deprecated and the use of LoaderManager
and CursorLoader
is recommended.
But delving into the LoaderManager
and CursorLoader
's use I found this example where inside an inner class that extends a ListFragment
(an extension of Fragment itself I suppose) we create a CursorLoader
. Everything seems ok, except for the fact that CursorLoader
takes a Uri
as an argument. So this implies that I need to create a ContentProvider
to get access to my database.
I must confess it looks like an overkill to have to go through all of this just to create a simple ListView
with items coming from a database. Specially if I have no intention of making my database data available to other apps, and the main purpose of a content provider is to do that.
So is it really worth it?
Especially in cases like mine where the content to be fetched is likely going to be small. I'm seriously considering doing it the old way, what do you say?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我编写了一个不需要内容提供程序的简单的CursorLoader:
它只需要
AsyncTaskLoader类。要么是Android 3.0以上版本,要么是兼容包自带的。
I wrote a simple CursorLoader that does not need a content provider:
It only needs the
AsyncTaskLoader
class. Either the one in Android 3.0 or higher, or the one that comes with the compatibility package.只需使用它下面的构造函数,即带有标志的构造函数。不要使用 FLAG_AUTO_REQUERY,只需为标志传递 0。
除非您确实需要在用户查看 ListView 时处理底层数据库的数据更改,否则您无需担心需要重新查询。
另一方面,如果您希望 ListView 在用户查看列表时显示对数据库的更改,请遵循 Google 的建议并使用 CursorLoader。
编辑:
由于第二个构造函数仅在 API 11 中可用,您可能只想自己扩展 CursorAdapter。您几乎只需要实现bindView 和newView 就可以了。
Just use the constructor below it, the one that takes the flags. Don't use the FLAG_AUTO_REQUERY, just pass 0 for the flags.
Unless you really need to handle data changes to the underlying DB while the user is looking at the ListView then you don't need to worry about needing to requery.
If on the other hand you want the ListView to show changes to the DB while the user is looking at the list then follow Google's advice and use the CursorLoader.
EDIT:
Since the second constructor is only available in API 11 you may just want to extend CursorAdapter yourself. You pretty much just need to implement bindView and newView and you are done.
仅使用 simpleCursorAdapter 已弃用的构造函数。出现这种错误的时候
我正在开发我的应用程序,但我使用了它,它与我的应用程序完美配合。或者尝试使用下面的构造函数,该构造函数在 android 开发者网站中已弃用,它有一个额外的参数,即带有 flag 参数。
Use simpleCursorAdapter deprecated constructor only. This kind of error appeared when
I was developing my app but i used it and it worked perfectly with my app. Or try to use the constructor below deprecated one in android developers website which has an extra argument i.e the flag argument with it.
我相信 CursorLoader 目前旨在与 ContentProvider 一起使用。
如果您希望使用新框架直接从数据库加载;您可以考虑扩展 AsyncTaskLoader 并从 onCreateLoader 返回它,而不是使用 CursorLoader。
如果您使用现有方法,则必须更加小心查询操作将花费多长时间。如果您的查询将花费大量时间,请考虑使用 AsyncTask 来加载游标(并注意 UI 线程中运行的重新查询)。
I believe CursorLoader is currently intended for use with a ContentProvider.
If you wish to load directly from your database using the new framework; you can consider extending AsyncTaskLoader and returning it from onCreateLoader instead of using a CursorLoader.
If you are using the existing methods you have to be more careful of how long your query operation will take. If your query will take noticable amounts of time consider using an AsyncTask to load the cursor (and be aware of requery running in the UI thread).
我知道这个线程很旧,但是您可以在 SimpleCursorAdapter 对象创建中添加最后一个参数。只需添加“,0”即可。
这是 Android 喜欢的标志,并且警告会消失。
I know this thread is old, but you could just add a last parameter into the SimpleCursorAdapter object creation. Just add ", 0".
It's a flag that Android likes and the warning goes away.