Cursor, LoaderManager and CursorLoader

What is Cursor?

Cursor is an scrollable result set, which contains results provided by the SELECT query.

Normally, when you make request for retrieving the data from the database using query or rawQuery the database returns the data in the form of Cursor.

The cursor allows us to read the columns that were returned from the database and iterate through them as well.

How to read data from the database?

SQLiteDatabase db = this.getReadableDatabase();

String[] columns = {"name", "PID", "Marks"};

Cursor c = db.query("students", columns, null, null, null, null, null};
int index;
while(c.moveToNext()){

index = c.getColumnIndex("name");
String name = c.getString(index);

index = c.getColumnIndex("PID");
String pid = c.getString(index);

}

This is how we can use cursor in Android Application, they are used with the SQLiteDatabase.

LoaderManager and CursorManager

The LoaderManager and CursorManager class provides an ability to run an SQL query in the background. When SQL query finishes the execution, it will automatically update the User Interface with that data.

The cursor will be automatically requeried and updates the UI, when there is a change in database which may affect the query.

When there is a configuration change, the LoaderManager will also requery the cursor.