29

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

Alex Lockwood
  • 81,274
  • 37
  • 197
  • 245
Jim
  • 7,796
  • 16
  • 63
  • 118

2 Answers2

22

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.

Community
  • 1
  • 1
Alex Lockwood
  • 81,274
  • 37
  • 197
  • 245
  • 2
    I think it's worth it to mention that the use of `ContentProvider`s is encouraged only if you plan to provide data to other apps. If you don't (i.e. the data is private to your app), then you are encouraged to use plain SQLite databases. For more info, check the docs for [ContentProvider](https://developer.android.com/reference/android/content/ContentProvider.html). – Felix Sep 16 '13 at 14:41
  • I think "to take the source of the CursorLoader class from the compatibility library, and ..." is the best choice, isn't it? – fikr4n Sep 20 '13 at 10:36
  • I had to extend `CursorLoader` with my own class to handle `OrmLite`. – theblang Nov 15 '13 at 15:26
0

Yes you can, You can have Custom data loaders which can load objects that you define or any object type or list in that matter.

Just look into the samples from the android sdk for the LoaderCustomSupport.java in the compatibility library samples and demos.

DArkO
  • 14,990
  • 10
  • 56
  • 82