Do I need a Content Provider with a Cursor Loader?
Asked Answered
A

3

19

I am having trouble working out how to use a CursorLoader.

The problem I am trying to solve is how to display data from my SQLite database in a ListActivity. The list rows use a custom layout so need data from 3 fields (all in the same table).

Along with a number of Stack Overflow question I also read this blog post and came to the decision that I needed to create a content provider for my database.

Having never used Content Providers before I went to the Android help on how to do this.

Point 1 states:

You don't need a provider to use an SQLite database if the use is entirely within your own application.

So now I am stuck, I can't seem to use a CursorLoader without a ContentProvider and I am advised against creating a ContentProvider if it is for use only within my application.

I am currently thinking that maybe the help is out of date or was not thinking of Cursor Loaders when it was written?

So do I need to just continue on and create a ContentProvider anyway or is there another way to do this? Any advice will be helpful!

Akene answered 21/8, 2012 at 16:3 Comment(1)
Also I should add that I am targeting SDK >= 14. <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="16" />Akene
A
8

After researching, I found that a ContentProvider IS needed if you are using the built in CursorLoader.

As wsanville says, you can roll your own Loader so that it does not need a ContentProvider.

Akene answered 18/1, 2013 at 14:40 Comment(0)
J
10

The short answer is that you can roll your own Loader to skip the need for a Content Provider, and interface directly with a SQLite database. Others have already done the implementation, one of which is done by CommonsWare, called LoaderEx. That project has a class called SQLiteCursorLoader which you may find useful.

In addition to the samples provided by the above GitHub project, you can check out one of my test applications (also on GitHub) using Loaders without a Content Provider.

Jerz answered 21/8, 2012 at 16:15 Comment(0)
A
8

After researching, I found that a ContentProvider IS needed if you are using the built in CursorLoader.

As wsanville says, you can roll your own Loader so that it does not need a ContentProvider.

Akene answered 18/1, 2013 at 14:40 Comment(0)
O
0

creating a ContentProvider is pretty easy. Easy enough that it shouldn't stand in your way of using a CursorLoader. Here's a quick example of using them together with a simple ContentProvider: http://responsiveandroid.com/2012/03/19/using-an-android-cursor-loader-with-a-content-provider.html

Orangewood answered 12/12, 2014 at 15:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.