Skip to main content

How to use Cursor Loader? Example

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?
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

        String[] a = { Boolean.toString(false) };
        return new CursorLoader(paramContext, Your.URI, ColumnNamesYouWantToQuery, null, null, "_id ASC ");
            This simple query return a cursor with list of element.
String[]{"Samsung Galaxy S6","HTC One M9","Sony Xperia Z5","LG G4", "Nexus 5X", "Nexus 6P"}
Other than this database can be filled from WEB or just pulling from existing content provider.
Let’s create a class with CursorLoader implementation.
package com.kozaxinan.sekreter;

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> {

    public void onCreate(Bundle savedInstanceState) {

    protected void onResume() {               
// if you want to restart loader
        getLoaderManager().restartLoader(0, null, this);

    public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
       //this is where we create background thread for cursor
        CursorLoader localCursorLoader = getCursorLoader(this);
        return localCursorLoader;

    public void onLoadFinished(Loader<Cursor> arg0, Cursor localCursor) {
        //make something with cursor and close it


    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.


Post a Comment

Popular posts from this blog

How to change position of MyLocationButton on new Map API v2 - 2

In my previuos post[1], i mention a workaround for changing buttons on Google Map which we use with google services. It is good to know they listen developers and make their products more development friendly. (I hope it goes like that way all the time and all the product lines of Google) Now we have a method for setting padding of map. It is add padding to useful area of map. We can see full screen map but with padding we can use prefered area of it. It is good for overlay action bar and other things on map view. [ 2] They write a blog and take a video about it. Read blog and watch video if you are using google map in your app :) [3] And finally, They add this method because of this issue. That means, if you find a bug or want extra feature from and api, write to google via this issue tracking system. [1] [2] [3] http://googlegeodeve

How to set alarm to show a notification at future date with AlarmManager?

Lets assume you want to do a job a future time or show a notification at a selected time. AlarmManager can help to send an intent in future time. First we need to select date, Lets create a date in 2050, Calendar calendar = Calendar. getInstance () ; calendar.set(Calendar. YEAR , 2050 ) ; Date date = calendar.getTime() ; For showing notification we need a service. public class AlarmReceiver extends Service { public AlarmReceiver() { } @Override public IBinder onBind (Intent intent) { } } Add service to Manifest.xml <service android :name= "com.kozaxinan.example.alarm.AlarmReceiver" android :enabled= "true" android :exported= "true" > </service> We will modify AlarmReceiver for sending notification. But first we need to register a alarm. AlarmManager alarmManager = (AlarmManager) getSystemService( ALARM_SERVICE ) ; Intent intent = new Intent( this, AlarmReceiver. class ) ; intent.putExtra( &q

Finally I released my First Game; The Red Button

Finally I released  "The Red Button Don't Touch It" . My First Game for Android. Game has only one objective; be fast enough to react and click the green button. Playstore Link : Please don't hesitate to give me feedback!