in Android

How to use Cursor Loader?

Why we need it?
Answer is simple, because it is a background thread. Cursor Loader doesn’t block UI thread like the it’s old way;
Activity.managerQuery.
Where we use it?
            It is effective tool when we have a large number of items in SQlite tables.
How we use?
            Setup;
We need a content provider to use Cursor Loader. Let’s write simple method for query database. Assume that database has some android phone. And in order to make it slow for take advange of Cursor Loader it will sleep 5 seconds in query.

public static CursorLoader getCursorLoader(Context paramContext) {
Log.i(TAG, “try to block query”);
Thread.sleep(5000); // miliseconds

return new CursorLoader(paramContext, Your.URI,
ColumnNamesYouWantToQuery, null, null, “_id ASC “);
}

This simple query return a cursor with list of element.
String[]{“Samsung Galaxy S4″,”HTC One”,”Sony Xperia Z”,”LG Nexus 4″}
Other than this database can be filled from WEB or just pulling from existing content provider.
            Usage;
Let’s create a class with CursorLoader implementation.
CursorLoaderActivity .java

package com.kozaxinan.sekreter;

import android.app.ListActivity;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

public class CursorLoaderActivity extends ListActivity implements
LoaderManager.LoaderCallbacks<Cursor> {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

@Override
protected void onResume() {
// cursor loader ı tekrar çalıştırmak için
getLoaderManager().restartLoader(0, null, this);
super.onResume();
}

@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
// Burada arkaplan işi olarak istenilen query çağırılır.

CursorLoader localCursorLoader = getCursorLoader(this);
return localCursorLoader;
}

@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor localCursor) {
// query bittikten sonra onunla istediğimizi yapacağımız yer burası
localCursor.close();
}

@Override
public void onLoaderReset(Loader<Cursor> arg0) {
}}

It is new API!!!
            CursorLoader is available before API level 11. But Android team add this good feature in the Compatibility Library Package.
            This API works in Fragment with Compatibility Library. Therefor we need a init method to call CursorLoader;
        getSupportLoaderManager().initLoader(LOADER_ID,null,this);
For this method we need a loader_id which we can select randomly.
This is a basic way to use CursorLoader. Use this API in your project to avoid blocked UI.
Sinan KOZAK
Elektronic Engineer

Related Posts

Written By:

2 Comments

  1. Said Tahsin Dane March 27, 2013 Reply
    • Sinan KOZAK March 27, 2013 Reply

Add a Comment

Your email address will not be published. Required fields are marked *