Click here to Skip to main content
11,432,168 members (60,518 online)
Click here to Skip to main content
Add your own
alternative version

Using Cursor Loader in Android

, 9 Oct 2014 CPOL
The article helps in understanding the use of Cursor Loader in Android.
customcursorloader.zip
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.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

public class DatabaseHandler extends SQLiteOpenHelper implements Runnable
{
	
	protected SQLiteDatabase mbDataBase;
	protected final Context mbContext;
    protected String db_name;
    protected SQLiteStatement insertStmt;
    protected static String DB_PATH = "/data/data/linkwithweb/databases/";
    protected static final String table_name="SampleCursorLoaderData";
    private static String TAG="DatabaseHandler";
    
    public static class UserTable {  
		   public static String id="_id";
		   public static String name="name";
	} 
	protected DatabaseHandler(Context context, String name, CursorFactory factory,int version){
		super(context, name, factory, version);
		this.mbContext = context;
		this.db_name = name;
		
		Thread t=new Thread(this);
		t.start();
		
		try{
			if(t.isAlive()){
				t.join();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void onCreate(SQLiteDatabase db){
		Log.v(TAG,"OnCreate method SQliteHelper");
		String str="create table "+table_name+" ( "+UserTable.id+" TEXT, "+ UserTable.name+" TEXT);";
		db.execSQL(str);
	}
	
	public void run(){
		mbDataBase = getWritableDatabase();
		Log.v(TAG,"Database object is created ="+mbDataBase);
	}

	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
		mbDataBase.execSQL("DROP TABLE IF EXISTS " + table_name);
		onCreate(mbDataBase);
	}
//	public synchronized void close() 
//	{
//		if (mbDataBase != null)
//		{
//			mbDataBase.close();
//		}
//		super.close();
//	}
	
	protected SQLiteDatabase getDb() 
	{
		return mbDataBase;
	}

	protected void insert() 
	{
		Log.v(TAG,"Inserting data in db");
		ContentValues cv=new ContentValues();
		cv.put(UserTable.id, 1);
		cv.put(UserTable.name, "Inserting Row 1");
		mbDataBase.insert(table_name,null, cv);
		
		cv=null;
		cv=new ContentValues();
		cv.put(UserTable.id, 2);
		cv.put(UserTable.name, "Inserting Row 2");
		mbDataBase.insert(table_name,null, cv);
		
		cv=new ContentValues();
		cv.put(UserTable.id, 3);
		cv.put(UserTable.name, "Inserting Row 3");
		mbDataBase.insert(table_name,null, cv);
		
		cv=new ContentValues();
		cv.put(UserTable.id, 4);
		cv.put(UserTable.name, "Inserting Row 4");
		mbDataBase.insert(table_name,null, cv);
	}
	public Cursor queryData(){
		
	    Cursor cursor = this.mbDataBase.query(table_name, null,null, null, null, null, null);
	    /*if (cursor != null && !cursor.isClosed()) {
	         cursor.close();
	    }*/
	    return cursor;
	}
}

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

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