Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need a Content Provider with a Cursor Loader?

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!

like image 554
John Avatar asked Aug 21 '12 16:08

John


People also ask

What is the use of cursor in content provider?

A Cursor object provides random read access to the rows and columns it contains. Using Cursor methods, you can iterate over the rows in the results, determine the data type of each column, get the data out of a column, and examine other properties of the results.

What is the purpose of content provider in Android?

Content providers can help an application manage access to data stored by itself, stored by other apps, and provide a way to share data with other apps. They encapsulate the data, and provide mechanisms for defining data security.

What is cursor loader?

A CursorLoader is a specialized member of Android's loader framework specifically designed to handle cursors. In a typical implementation, a CursorLoader uses a ContentProvider to run a query against a database, then returns the cursor produced from the ContentProvider back to an activity or fragment.

What is a content provider how exploitable is it great learning?

To the system, a content provider is an entry point into an application for publishing defined data objects, distinguished by a URI design. Thus an application can decide how it requires to draft the data it holds to a URI namespace, giving out those URIs to other objects which can, in turn, use them to reach the data.


2 Answers

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.

like image 197
wsanville Avatar answered Nov 15 '22 06:11

wsanville


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.

like image 20
John Avatar answered Nov 15 '22 05:11

John