The google docs point out not to use the CursorAdapters
first constructor,
CursorAdapter(Context context, Cursor c)
There are
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?
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 it
FLAG_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.