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?
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.
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.
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.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.
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).