1

I'm new to Android and have successfully implemented an ArrayAdapter to display a list of simple objects in a ListView. I have also created a class that extends SQLiteOpenHelper and I'd like to use this to display a list of rows from the database in the ListView.

From what I can tell, it seems like I should be using a Loader to asynchronously query the data, and act as a middle man between my data and the UI.

The Loader and CursorLoader documentation only refers to how to achieve this when querying a ContentProvider. This led me to think that the best approach may be to create a ContentProvider which provides a structured interface to my database - but the Android documentation on creating a ContentProvider states:

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

Are there any particular reasons why I shouldn't write a ContentProvider? Unless I'm missing something it seems like this would provide a good abstraction for the data layer and mean I can have all the benefits of using a CursorLoader when consuming it.

In my case the database is only for use by my application - so what alternatives are there and are there any good tutorials which show the process start to finish?

Thanks!

Matt Wilson
  • 7,359
  • 7
  • 28
  • 51
  • see this http://stackoverflow.com/questions/11017646/android-cursorloader-loadermanager-sqlite – pskink Jan 09 '14 at 11:29
  • you can use a cursorloader even if your database isnt wrapped in a contentprovider – Daniel Bo Jan 09 '14 at 11:42
  • @pskink - Thanks I will read this in more detail now. It seems strange to me that you have to write your own subclass of CusorLoader. It seems like a very common thing that people would want. – Matt Wilson Jan 09 '14 at 11:52
  • @Daniel - how so? The cursor loader seems to require a URI which would only make sense with a ContentProvider? – Matt Wilson Jan 09 '14 at 11:53
  • @MattWilson so it is better to write a ContentProvider, maybe a bit more effort but it pays off – pskink Jan 09 '14 at 11:58
  • @pskink this was my thinking - it seems like quite a good way to do it as it would provide good structure? I guess my fear is that it seems wrong to directly go against the advice about not creating a ContentProvider for "private" data. – Matt Wilson Jan 09 '14 at 12:01
  • ContentProvider also helps in keeping cursors up to date when inserting, deleting and updating the sqlitedb – pskink Jan 09 '14 at 12:12

0 Answers0