16

I'm new to Android development. I'm trying to create an application that reads from the internal database (SQLite) and list all the data in a list (I'm using listView).

So far I got a class called DatabaseHandler that extends SQLiteOpenHelper and that is doing all the database operations (select data, insert data, delete data, ...).

But now that I want to list the values, I am reading in some websites that I have to use a Loader instead of Cursor, and therefore a ContentProvider. So far I understand that ContentProvider provides controlled access to the database.

My question is: does the ContentProvider do the same as SQLiteOpenHelper?

Also, I'm using API level 8 and the ContentProvider is only available on API level 11. What is the best way to solve this?

Thanks in advance.

Quasaur
  • 1,177
  • 1
  • 9
  • 27
André Alves
  • 5,605
  • 3
  • 15
  • 22
  • 1
    Can't answer on the *best* way, but the *pragmatic* way is to use a Cursor. It still works perfectly well, and you save a lot of development time if you don't need the additional features provided by the ContentProvider. – Heinzi Nov 27 '12 at 14:24
  • @Heinzi It would be good to use Cursor as they seem simpler. The problem is that when using `startmanagingcursor(cursor)` it says it's deprecated. Also I keep reading that loaders are better due to the data synchronization issues... – André Alves Nov 27 '12 at 14:29
  • 3
    [SQLiteOpenHelper is a helper class intended to use within ContentProvider](http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html) – Selvin Nov 27 '12 at 14:41
  • 1
    `Also, I'm using API level 8 and the ContentProvider is only available on API level 11. What is the best` not true ... `Loader`s are from API 11 but we have support library v4 ... `ContentProvider`s are from API 1 :) – Selvin Nov 27 '12 at 14:43
  • You're right! I totally mixed them up! :P So the best solution is to use support libraries? – André Alves Nov 27 '12 at 14:53

2 Answers2

10

My question is: does the ContentProvider do the same as SQLiteOpenHelper?

ContentProvider is implemented by application's developer if he or she would allow other developers to access to application's database in their application - simply put for sharing. It's like a server of some database and it's client is ContentResolver who knows ContentProvider's authority. For example if you need to get some contacts from your device, than you should use ContentProvider of Contacts database and more concretely it's Contract classes.

If you know an authority of appropriate ContentProvider you may comunicate with it using a ContentResolver object.

In other cases you should interact with database through SQL abstract model which is represented by android.database and android.database.sqlite classes.

And also - ContentProvider available from the primary API level as one of the main component of application.

Update

From the official documentation:

Before you start building a provider, do the following:

Decide if you need a content provider. You need to build a content provider if you want to provide one or more of the following features:

  • You want to offer complex data or files to other applications.
  • You want to allow users to copy complex data from your app into other apps.
  • You want to provide custom search suggestions using the search framework.
  • Alex Bonel
    • 1,126
    • 1
    • 8
    • 21
    • This is the main purpose of `ContentProvider` - to export it to other applications. If you don't need to export your data to another applications than you shouldn't consider to use `ContentProvider`. – Alex Bonel Nov 27 '12 at 14:46
    • No... main purpose is [to manage access to a structured set of data](http://developer.android.com/guide/topics/providers/content-providers.html)(i don't see single "export" word there). I can even say ... this is prefered way of accesing data(even SQLite) on android! – Selvin Nov 27 '12 at 14:48
    • In my case I don't want to export to other applications. I just want to access database data internally and list it. The problem is that when I am trying to use `Cursor` and `startmanagingcursor(cursor)`, it says that this is deprecated and that I should replace it by a Loader. And as far as I know, a Loader needs a `ContentProvider`, that's why I am confused... – André Alves Nov 27 '12 at 14:51
    • Edited my answer for better explanation basing on official documentation – Alex Bonel Nov 27 '12 at 14:56
    • So what should I do if I don't want to use ContentProvider (because I don't need it) but I want to list all the data in the database? – André Alves Nov 27 '12 at 15:00
    • 11
      if you wana: **no auto requery support, cursors leaking, sqlopenhelper leak, manual guarding for multiple threads access to db, no Loaders support** then **you can use plain SQLite** ... – Selvin Nov 27 '12 at 15:12
    • 7
      Intention of `ContentProvider` was to serve data to other processes via a defined interface. But things changed with Honeycomb. All the deprecated methods like `startManagingCursor` tell you to use a Loader now. A `CursorLoader` does not work without a `ContentProvider` and `Uri` based change notifications that cause automatic reloading in a background thread (#1 reason for Loaders) won't work either. There are probably ways around but `ContentProvider` is definitely the way you should consider now. also http://www.androiddesignpatterns.com/2012/07/loaders-and-loadermanager-background.html – zapl Nov 27 '12 at 15:37
    • 1
      So I guess I need a `ContentProvider` then... Thanks a lot for your answers! :) – André Alves Nov 27 '12 at 15:44
    8

    SQLiteOpenHelper is used to manage database creation and open. It is only a helper class for sqlite database and could be used in anywhere with db operation.

    ContentProvider is one of the 4 basic components of android, the other 3 are Activity, Service and Broadcast. ContentProvider is used to manage access to any structured data set, including sqlite database. The data source of ContentProvider could be a databse but not have to be.

    ContentProvider is often used in providing own data to others applications, just like ContactsProvider and CalendarProvider. And it is more safe to provide some specified interface using ContentProvider, compared with providing a database directly.

    Reza
    • 1,788
    • 2
    • 17
    • 31
    faylon
    • 7,192
    • 1
    • 28
    • 28