解决android - What to set CursorAdapter(Context context, Cursor c, int flags) to in order to make it work with CursorLoader?
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)
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.`
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
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
CursorAdapter? Goal is to not cause any memory leaks and be efficient.
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
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.
Which constructor should I 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
stopManagingCursor methods were deprecated.
... I could just do
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
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
LoaderManageris 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
- 1Android Https相关完全解析 当OkHttp遇到Https
- 2Android APK反编译详解（附图）
- 3[置顶] Android APK反编译就这么简单 详解（附图）
- 4Android Fragment 真正的完全解析（上）
- 5Android RecyclerView 使用完全解析 体验艺术般的控件