ITKeyword,专注技术干货聚合推荐

注册 | 登录

解决android - What to set CursorAdapter(Context context, Cursor c, int flags) to in order to make it work with CursorLoader?

itPublisher 分享于

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

The google docs point out not to use the CursorAdapters first constructor,

CursorAdapter(Context context, Cursor c)

There are only two other options,

CursorAdapter(Context context, Cursor c, boolean autoRequery)

which says

Constructor that allows control over auto-requery. It is recommended you not use this, but instead CursorAdapter(Context, Cursor, int). When using this constructor, FLAG_REGISTER_CONTENT_OBSERVER will always be set.`

and

CursorAdapter(Context context, Cursor c, int flags)`

which says it is the recommended constructor.

Problem is there are only two flags to use with the last constructor here, FLAG_AUTO_REQUERY(int 1) and FLAG_REGISTER_CONTENT_OBSERVER(int 2). Using FLAG_AUTO_REQUERY doesn't make sense because I am now using a CursorLoader in which to manage it in the background as well as update it. With FLAG_REGISTER_CONTENT_OBSERVER, it says its not needed when using CursorLoader.

Now I ask, what integer do I pass CursorAdapter(Context context, Cursor c, int flags) in order to make it work fine with my CursorAdapter? Whats worrying me is how to correctly manage the old cursor. I am not really sure the correct way to do this.

If I use FLAG_REGISTER_CONTENT_OBSERVER, then I must do something with onContentChanged(), but when using swapCursor() in my LoaderManager, since the cursor is not closed, I could just do adapter.swapCursor(cursor).close(). But would that conflict with onContentChanged() in CursorAdapter? Goal is to not cause any memory leaks and be efficient.

android android-contentprovider android-cursoradapter android-loadermanager android-cursorloader
|
  this question
edited Jun 19 '12 at 3:17 Alex Lockwood 65k 27 167 219 asked Jun 19 '12 at 1:49 Andy 4,665 13 58 99

 | 

1 Answers
1

解决方法

I blogged about this topic a couple weeks ago... maybe reading through it will help. You might also consider reading through the sample code on the developers site.

Reaping the Benefits of the LoaderManager Class

Which constructor should I use?

Use CursorAdapter(Context context, Cursor c, int flags) (the documentation recommends using this constructor over the former).

What integer do I pass CursorAdapter(Context context, Cursor c, int flags).

Just pass it the integer 0. You don't want to pass it FLAG_REGISTER_CONTENT_OBSERVER, since you are using a CursorLoader with your CursorAdapter (since the CursorLoader registers the ContentObserver for you), and you definitely don't want to pass itFLAG_AUTO_REQUERY` since that flag is deprecated.

What's worrying me is how to correctly manage the old cursor. I am not really sure the correct way to do this.

The whole point of the LoaderManager is that it does all of the annoying cursor management stuff for you, behind the scenes. The convenience of having your data loaded automatically without having to worry about managing the queried cursor is precisely why the old startManagingCursor and stopManagingCursor methods were deprecated.

... I could just do adapter.swapCursor(cursor).close()

Don't do that. The LoaderManager will close the cursor on its own. In fact, if I remember correctly, you will get an error if you attempt to call close() on the cursor. It sounds like you shouldn't have to override onContentChanged() either.


|
  this answer
edited Jun 19 '12 at 3:24 answered Jun 19 '12 at 3:16 Alex Lockwood 65k 27 167 219      Lmao, you just got all the answers man! Thanks. And I will read your blog on that. Thank you! –  Andy Jun 19 '12 at 3:39      LoaderManager is my specialty... it's also my favorite class in the Android framework :). I'm looking forward to answering your next question :P –  Alex Lockwood Jun 19 '12 at 3:41      Touche my friend –  Andy Jun 19 '12 at 3:46 2   It should be defined a CursorAdapter.FLAG_NONE = 0 in the API... Thank you @AlexLockwood –  caligari Aug 7 '13 at 13:02

 | 


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。