Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: CursorLoader, LoaderManager, SQLite

Trying to update my old app in which some methods are deprecated. I found out that if I want to work with ListView that shows data from db, I should use LoaderManager + CursorLoader. CursorLoader works with content providers. So for every table in my db I should create content provider now ? Why should I ? As far as I know content providers is used to share some db information with other applications, but my app dont share any information. So can I use CursorLoader without content providers ???

like image 394
Jim Avatar asked Jun 13 '12 14:06

Jim


1 Answers

I've written a blog post on this subject. You can also check out this answer for more information. Hopefully it will clear things up for you.

As Barak mentioned, one can implement a CursorLoader without content providers by extending AsyncTaskLoader<Cursor> class. That said, most of the tutorials and sample code use ContentProviders, and it seems like the Android team encourages its use as well. It's also a lot less complicated than implementing your own class.

That said, if you really don't want to use content providers, Dianne Hackborn (one of the Android framework developers, and also known as "hackbod" here on SO) suggests writing your own loader that uses your database class instead of a content provider. The easiest way is just to take the source of the CursorLoader class from the compatibility library, and replace provider queries with queries to your own db helper class.

like image 120
Alex Lockwood Avatar answered Sep 20 '22 23:09

Alex Lockwood