Search code examples
androidsqldatabaseandroid-cursorloader

How do I go about implementing CursorLoader without using a Listview?


after going through loads of tutorials on CursorLoader for using with a SQLiteDatabase, I'm still a bit confused. Being a beginner, I was unaware of the advantages of Listview and instead chose to use several ViewGroups in a vertical order in my first app.

Now that my app is complete and I want to optimize it by using a CursorLoader for doing all the SQL operations (CRUD), I don't have any idea how to implement it.

Is it possible to extract the information from the returned adapter and update the TextViews inside the ViewGroups?

Furthermore, I'm using a DatabaseHelper for all my SQL requirements. Do I need to re-write it to use a Loader? How tough would it be?

Sorry if this is a naive question, but the official documentation are not at all clear.


Solution

  • Is it possible to extract the information from the returned adapter

    There is no "returned adapter". CursorLoader is a Loader that returns a Cursor. None of those things are "adapters" as the term is used in Android.

    and update the TextViews inside the ViewGroups?

    Yes. The Cursor API allows you to set which row you want to work with (moveToPosition(), etc.), from which you can retrieve columns (getString(), etc.).

    Do I need to re-write it to use a Loader?

    CursorLoader works only with a ContentProvider. If your DatabaseHelper (presumably a subclass of SQLiteOpenHelper) is used by a ContentProvider, then your CursorLoader can work with that ContentProvider. Other alternatives include:

    • not bothering with the Loader framework, and just using AsyncTask and kin for doing your database I/O in the background

    • using my SQLiteCursorLoader, which works directly with a SQLiteOpenHelper