ContentProvider in Android

I already told about the built-in content-providers in Android, in one of my tutorial

In this, I am creating a new custom content provider. For this, first we will declare the content provider in Manifest file as,

<provider android:name="Provider" android:authorities="simple.provider"/>

So,Provider is my content provider name. Created a class called Provider which extends ContentProvider. The CONTENT_URI, which I am going to use is, "content://simple.provider/mytable"

The complete source code is,

Provider.java

 
public class Provider extends ContentProvider {

public static final String AUTHORITY = "simple.provider";
// public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
// + "/table1");
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
+ "/mytable");
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.provider.mytable";
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.provider.mytable";
public static final String DATABASE_NAME = "MyDatabase";
public static final String TABLE_NAME = "mytable";
public static final int DATABASE_VERSION = 2;
public static final int TABLE = 1;
public static final int ROW_ID = 2;
public static final UriMatcher sUriMatcher;
public static final String FIRSTNAME = "first_name";
public static final String LASTNAME = "last_name";
private SQLiteDatabase sqlitedb;

static {
sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
sUriMatcher.addURI(AUTHORITY, "mytable", TABLE);
sUriMatcher.addURI(AUTHORITY, "mytable/#", ROW_ID);
}

class DatabaseHelper extends SQLiteOpenHelper {

public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
db
.execSQL("CREATE TABLE IF NOT EXISTS mytable "
+ "(id INTEGER PRIMARY KEY AUTOINCREMENT, first_name VARCHAR,"
+ "last_name VARCHAR);");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS mytable;");
onCreate(db);
}
}

// creating database
@Override
public boolean onCreate() {
DatabaseHelper databaseHelper = new DatabaseHelper(getContext());
sqlitedb = databaseHelper.getWritableDatabase();
return (sqlitedb == null) ? false : true;
}

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

@Override
public String getType(Uri uri) {
switch (sUriMatcher.match(uri)) {
case TABLE:
return CONTENT_TYPE;
case ROW_ID:
return CONTENT_ITEM_TYPE;
default:
throw new IllegalArgumentException("Unknown URI : " + uri);
}
}

// inserting data into database
@Override
public Uri insert(Uri uri, ContentValues values) {
long row_id = sqlitedb.insert(TABLE_NAME, "", values);
try {
if (row_id > 0) {
Uri uri1 = ContentUris.withAppendedId(CONTENT_URI, row_id);
getContext().getContentResolver().notifyChange(uri1, null);
return uri1;
}
} catch (Exception e) {
Log.e("Fail to insert the row into database",
"Fail to insert the row into database");
}
return null;
}

// accessing records from database
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder sqlBuilder = new SQLiteQueryBuilder();
sqlBuilder.setTables(TABLE_NAME);
if (sUriMatcher.match(uri) == ROW_ID)
sqlBuilder.appendWhere("id" + " = " + uri.getPathSegments().get(1));
Cursor c = sqlBuilder.query(sqlitedb, projection, selection,
selectionArgs, null, null, sortOrder);
// ---register to watch a content URI for changes---
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}

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



To accessing the ContentProvider, use ContentResolver. We can get the object using getContentResolver()

The complete source code for this is,

SimpleProvider.java

 
public class SimpleProvider extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ContentValues values = new ContentValues();
values.put(Provider.FIRSTNAME, "Nithin");
values.put(Provider.LASTNAME, "Warrier");
getContentResolver().insert(Provider.CONTENT_URI, values);

ContentValues values1 = new ContentValues();
values1.put(Provider.FIRSTNAME, "Guna");
values1.put(Provider.LASTNAME, "Boss");
getContentResolver().insert(Provider.CONTENT_URI, values1);

Cursor c = managedQuery(Provider.CONTENT_URI, null, null, null, null);
int count = 1;
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
Log.i("Row " + count + " "
+ c.getString(c.getColumnIndex(Provider.FIRSTNAME)), c
.getString(c.getColumnIndex(Provider.LASTNAME)));
count++;
}
}
}

Comments

Popular posts from this blog

AlarmManager and Notification in Android

Swipe Action and ViewFlipper in Android