Tutorial details

Android beginner tutorial Part 68 - Working with database using Content Provider | App Code for Sale | Preview

Android beginner tutorial Part 68 - Working with database using Content Provider | Android Tutorial

how to add code to our Content Provider class that will let us work with the database

Overview PAGE TOP

In this tutorial we will add code to our Content Provider class that will let us work with the database.

In the previous tutorial we've discussed how Content Provider works, what it is needed for and how to create it. If you haven't yet created a Content Provider class in your application, then do it now. I mentioned before that it has to include a number of methods. Today we'll add code to those methods to give the Content Provider some basic functionality.

First of all, we'll declare the CONTENT_URI variable which contains the URI address of this content provider. After that, declare a SQLiteDatabase instance called db.

public class myContentProvider extends ContentProvider {
private static final Uri CONTENT_URI = Uri.parse("content://com.kircode.codeforfood_test.contactprovider/contacts");
private SQLiteDatabase db;

Now go to the onCreate() function and set db's value to a new myDbHelper instance's getWritableDatabase() returned value (the class we've created before):

@Override
public boolean onCreate() {
db = new myDbHelper(getContext()).getWritableDatabase();
return (db == null) ? false : true;
}

Edit the query() function to return a Cursor object (a Cursor is used to access table data) by using the data received in the parameters, after processing them a bit.

The parameters that we receive ar url, columns, selection, selectionArgs and sort.

First we declare a variable orderBy and set its value based on whether the sort parameter is empty or not. The sort parameter's value is what would usually stand after "ORDER BY" in an SQL query. We need to give orderBy a default value if sort isn't set.

After the value of orderBy is set, create a Cursor object using db.query() method. It would be more correct to say: call the query() method of db, which also returns a Cursor. Use the all the data we have to pass onto that query() method. After that, set the uri of the Cursor object using its setNotificationUri() method. Use the uri that's passed to the function as the first parameter:

@Override
public Cursor query(Uri url, String[] columns, String selection, String[] selectionArgs,
String sort) {
String orderBy;

if(TextUtils.isEmpty(sort)){
orderBy = myDbHelper.NAME;
}else{
orderBy = sort;
}

Cursor c = db.query(myDbHelper.TABLE_NAME, columns, selection, selectionArgs, null, null, orderBy);
c.setNotificationUri(getContext().getContentResolver(), url);
return c;
}

The next function we're going to add is insert(). Here we will also call the method of the same name, which requires 3 parameters - table, nullColumnHack and values. We can get the data from the two parameters that are passed to the function and from myDbHelper class.

The insert() method of the database returns the new row's id. If that value is greather than 0, we can say that the operation was sucessfull and call the notifyChange() method of the ContentResolver object by passing the uri to it. Otherwise, throw an exception:

@Override
public Uri insert(Uri url, ContentValues inValues) {
ContentValues values = new ContentValues(inValues);
long rowId = db.insert(myDbHelper.TABLE_NAME, myDbHelper.NAME, values);

if(rowId > 0){
Uri uri = ContentUris.withAppendedId(CONTENT_URI, rowId);
getContext().getContentResolver().notifyChange(uri, null);

return uri;
}else{
throw new SQLException("Failed to insert row into " + url);
}
}

The next function is update(). This one's simple - simply call database's update() method and pass the needed values, then call notifyChange() using the uri like we just did:

@Override
public int update(Uri url, ContentValues values, String where, String[] whereArgs) {
int retVal = db.update(myDbHelper.TABLE_NAME, values, where, whereArgs);
getContext().getContentResolver().notifyChange(url, null);

return retVal;
}

The delete() function is similar:

@Override
public int delete(Uri url, String where, String[] whereArgs) {
int retVal = db.delete(myDbHelper.TABLE_NAME, where, whereArgs);
getContext().getContentResolver().notifyChange(url, null);

return retVal;
}

And the full class code now looks like this:

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.text.TextUtils;

import com.kircode.codeforfood_test.myDbHelper;


public class myContentProvider extends ContentProvider {
private static final Uri CONTENT_URI = Uri.parse("content://com.kircode.codeforfood_test.contactprovider/contacts");
private SQLiteDatabase db;

public myContentProvider() {
// TODO Auto-generated constructor stub
}

@Override
public int delete(Uri url, String where, String[] whereArgs) {
int retVal = db.delete(myDbHelper.TABLE_NAME, where, whereArgs);
getContext().getContentResolver().notifyChange(url, null);

return retVal;
}

@Override
public String getType(Uri arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public Uri insert(Uri url, ContentValues inValues) {
ContentValues values = new ContentValues(inValues);
long rowId = db.insert(myDbHelper.TABLE_NAME, myDbHelper.NAME, values);

if(rowId > 0){
Uri uri = ContentUris.withAppendedId(CONTENT_URI, rowId);
getContext().getContentResolver().notifyChange(uri, null);

return uri;
}else{
throw new SQLException("Failed to insert row into " + url);
}
}

@Override
public boolean onCreate() {
db = new myDbHelper(getContext()).getWritableDatabase();
return (db == null) ? false : true;
}

@Override
public Cursor query(Uri url, String[] columns, String selection, String[] selectionArgs,
String sort) {
String orderBy;

if(TextUtils.isEmpty(sort)){
orderBy = myDbHelper.NAME;
}else{
orderBy = sort;
}

Cursor c = db.query(myDbHelper.TABLE_NAME, columns, selection, selectionArgs, null, null, orderBy);
c.setNotificationUri(getContext().getContentResolver(), url);
return c;
}

@Override
public int update(Uri url, ContentValues values, String where, String[] whereArgs) {
int retVal = db.update(myDbHelper.TABLE_NAME, values, where, whereArgs);
getContext().getContentResolver().notifyChange(url, null);

return retVal;
}

}

That's all for today.

Thanks for reading!

Reference PAGE TOP

http://kirill-poletaev.blogspot.it/2013/01/android-beginner-tutorial-part-68.html

0 Comments Leave a comment

Please login in order to leave a comment.

Newest first
!

Sign-in to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.