Apr 19, 2012

Create custom or own content Provider

Hey everyone,
So the following tutorial will hopefully give you a good idea of how to implement your own ContentProvider. I know that there are a lot of pretty good sites out there with some good code snippets, but I’ve noticed that not many really help the developer understand what’s going on step by step so my goal is to not only provide an example of a fully implemented ContentProvider but also to step you through the process.
So I’ll start by just posting all of the code, and I’ll go through it slowly afterwards. We’ll start with the Custom Content Provider itself:

package com.bened;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

/*
* Creating Custom Content provider
* to create custom content provider
*
* create a class that class should be extends ContentProvider
* and override insert , oncreate , query , update methods ,
*/
public class MyProvider extends ContentProvider {
// CONTENT_URI : BASE URL FOR CONTENT PROVIDER

public static final Uri CONTENT_URI = Uri
.parse("content://com.bened/contenttable");
private MyDataBase mdb;
private SQLiteDatabase db;

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return 0;
}

/*
*
* get URL type
*/
@Override
public String getType(Uri uri) {
return null;
}

/*
*
* inserting values to database
*/

@Override
public Uri insert(Uri uri, ContentValues values) {
db.insert("contenttable", null, values);
return null;
}

/*
* allocating memory to database and giving permissions to database
*/
@Override
public boolean onCreate() {
mdb = new MyDataBase(getContext(), "ContentData", null, 2);
db = mdb.getWritableDatabase();
return true;
}

/*
*
* TO fetch values from db
*/
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
String col[] = { "_id", "no", "name", "email" };
Cursor c = db.query("contenttable", col, null, null, null, null, null);
return c;
}

/*
*
* for updating values to database
*/
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
return 0;
}

}


Okay so let’s go through this step by step. First, when you extend ContentResolver, there are 6 methods you need to overwrite:
query()
insert()
update()
delete()
getType()
onCreate()

Normally these are just wrapper functions around the raw SQL queries. For instance, the method:

db.delete(tableName, where, whereArgs);

"delete from " + tableName + " where " + where + " ? " + whereArgs"


So for those who know SQL, if I wanted to delete the note with title “Hello World” then my queries would look like:

// wrapper query
db.delete(NOTES_TABLE_NAME, Notes.TITLE + "= ' " + "Hello World" + " ' ", null);

// real query when translated
String query = "delete from notes where title = 'Hello World' ";
And so as you look at how I overwrite the 6 methods, you’ll see that I’m simply taking in the parameters and inputting them appropriately into the wrapper methods. Of course, you can override them as you like depending on what you want your application to do. Perhaps your application is ONLY worried about retrieving the average of some numbers. Then in your query() method, maybe instead of querying for the numbers normally, and then always having to iterate through the numbers and find the average from the JAVA side, you could customize your query to automatically return the average BY DEFAULT

But in case you’re still confused, let’s look at an example:

create a table with specified 4 columns

@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
String col[] = { "_id", "no", "name", "email" };
Cursor c = db.query("contenttable", col, null, null, null, null, null);
return c;
}


One thing to note here is that Android requires that the unique id column have name “_id”

This is extremely important as otherwise your content provider will not get registered properly.
Now, one last thing before I get to how you register it in your Manifest is what this AUTHORITY String is all about. In my case it was:

public static final String CONTENT_URI = "path ";


And so this AUTHORITY basically just helps you define the CONTENT_URI of your ContentProvider (i.e. the path to your database), and you will register your provider in your Manifest like so:

android:exported="true"
android:name="MyProvider"
android:authorities="com.bened">

note : name indicates content provider name
authotities : mainl what uri you are using as a path


And so you see that I define my content providers in a way such that the project path is equal to the authorities path (again, not required, but probably recommended for simplicity and organizational issues).
And that’s it! Now you have a fully registered and created ContentProvider.








for source clik here :

ContentProvider Link1


Content provider Link2


Download both projects
1-> run first project content provider one demo  -> insert some values in that
2-> run content provider two demo we can view which is inserted first value .

1 comment:

  1. your tutorial helped to me a lot regarding content providers it is simple and helpful to understand

    ReplyDelete