Click here to Skip to main content
12,633,764 members (31,625 online)
Click here to Skip to main content

Stats

77.4K views
1.7K downloads
18 bookmarked
Posted

Using Cursor Loader in Android

, 9 Oct 2014 CPOL
The article helps in understanding the use of Cursor Loader in Android.
CustomCursorLoader
.classpath
.project
.settings
org.eclipse.jdt.core.prefs
assets
bin
gen
tpg
main
ic_launcher-web.png
libs
android-support-v4.jar
project.properties
res
drawable-hdpi
ic_launcher.png
drawable-ldpi
ic_launcher.png
drawable-mdpi
ic_launcher.png
drawable-xhdpi
ic_launcher.png
layout
values
values-v11
values-v14
src
tpg
database
main
package tpg.main;

import tpg.database.DatabaseAccessUtility;
import tpg.database.DatabaseHandler;
import android.app.ListActivity;
import android.app.LoaderManager;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends ListActivity implements LoaderCallbacks<Cursor>  {

	
    SimpleCursorAdapter mAdapter; 		
    LoaderManager loadermanager;		
    CursorLoader cursorLoader;
    private static String TAG="CursorLoader";
    
    protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		loadermanager=getLoaderManager();
		
		String[] uiBindFrom = {  DatabaseHandler.UserTable.name};		
	    int[] uiBindTo = {android.R.id.text1};
	    /*Empty adapter that is used to display the loaded data*/
	    mAdapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1, null, uiBindFrom, uiBindTo,0);  
        setListAdapter(mAdapter);
       
        /**
         * This initializes the loader and makes it active. If the loader specified by the ID already exists, the last created loader is reused.
         * If the loader specified by the ID does not exist, initLoader() triggers the LoaderManager.LoaderCallbacks method onCreateLoader(). 
         * This is where you implement the code to instantiate and return a new loader. 
         * Use restartLoader() instead of this, to discard the old data and restart the Loader.
         * Hence, here the given LoaderManager.LoaderCallbacks implementation are associated with the loader. 
         */
        loadermanager.initLoader(1, null, this);
       
	}
	
    /**
     * This creates and return a new Loader (CursorLoader or custom Loader) for the given ID. This method returns the Loader that is created, 
     * but you don't need to capture a reference to it. 
     */
	public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
		
		String[] projection = { DatabaseHandler.UserTable.id, DatabaseHandler.UserTable.name };
		
		/**
		 * This requires the URI of the Content Provider
		 * projection is the list of columns of the database to return. Null will return all the columns
		 * selection is the filter which declares which rows to return. Null will return all the rows for the given URI.
		 * selectionArgs:  You may include ?s in the selection, which will be replaced by the values from selectionArgs, in the order that they appear in the selection. 
		 * The values will be bound as Strings.
		 * sortOrder determines the order of rows. Passing null will use the default sort order, which may be unordered.
		 * To back a ListView with a Cursor, the cursor must contain a column named _ID.
		 */
		cursorLoader = new CursorLoader(this, DatabaseAccessUtility.CONTENT_URI, projection, null, null, null);
	    return cursorLoader;
		
	}

	/**
	 * Called when a previously created loader has finished its load. This assigns the new Cursor but does not close the previous one. 
	 * This allows the system to keep track of the Cursor and manage it for us, optimizing where appropriate. This method is guaranteed
	 * to be called prior to the release of the last data that was supplied for this loader. At this point you should remove all use of 
	 * the old data (since it will be released soon), but should not do your own release of the data since its loader owns it and will take care of that.
	 * The framework would take of closing of old cursor once we return.
	 */
	public void onLoadFinished(Loader<Cursor> loader,Cursor cursor) {
		if(mAdapter!=null && cursor!=null)
			mAdapter.swapCursor(cursor); //swap the new cursor in.
		else
			Log.v(TAG,"OnLoadFinished: mAdapter is null");
	}
	/**
	 * This method is triggered when the loader is being reset and the loader data is no longer available. 
	 * This is called when the last Cursor provided to onLoadFinished() above is about to be closed.  We need to make sure we are no longer using it.
	 */
	public void onLoaderReset(Loader<Cursor> arg0) {
		if(mAdapter!=null)
			mAdapter.swapCursor(null);
		else
			Log.v(TAG,"OnLoadFinished: mAdapter is null");
	}

	
	
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Khushboo Kaur
Software Developer 3Pillar Global, Inc
India India
No Biography provided

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161208.2 | Last Updated 9 Oct 2014
Article Copyright 2013 by Khushboo Kaur
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid