How to read an SQLite DB in android with a cursorloader?

Android Guide suggests to create a ContentProvider when you want to share your data with other applications. If you don’t need this, you can just override method loadInBackgroud() of the CursorLoader class. For example write like this in your onCreateLoader:

return new CursorLoader( YourContext, null, YourProjection, YourSelection, YourSelectionArgs, YourOrder )
           {
               @Override
               public Cursor loadInBackground()
               {
                   // You better know how to get your database.
                   SQLiteDatabase DB = getReadableDatabase();
                   // You can use any query that returns a cursor.
                   return DB.query( YourTableName, getProjection(), getSelection(), getSelectionArgs(), null, null, getSortOrder(), null );
               }
           };

Leave a Comment