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

后端 未结 1 1662
醉梦人生
醉梦人生 2020-12-28 19:32

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

CursorAdapter(Context context, Cursor c)

There are

相关标签:
1条回答
  • 2020-12-28 20:15

    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.

    0 讨论(0)
提交回复
热议问题