Implement the business login in the override methods.
Using the onCreate Method we can initilze the Database
@Override
public boolean onCreate() {
Context context = getContext();
SQLiteConnectionManager dbHelper = new SQLiteConnectionManager(context);
demoDB = dbHelper.getWritableDatabase();
return (demoDB == null) ? false : true;
}
getType method is used to validate the input url
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)) {
case UserInfo.USERINFO:
return UserInfo.CONTENT_TYPE;
case UserInfo.USERINFO_ID:
return UserInfo.CONTENT_ITEM_TYPE;
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
}
CRUD Operation
Create - insert()
long rowID = demoDB.insert(UserInfo.DATABASE_TABLE, "", values);
if (rowID > 0) {
Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
getContext().getContentResolver().notifyChange(_uri, null);
return _uri;
}
Retrive - query()
SQLiteQueryBuilder sqlBuilder = new SQLiteQueryBuilder();
sqlBuilder.setTables(UserInfo.DATABASE_TABLE);
if (uriMatcher.match(uri) == UserInfo.USERINFO_ID)
sqlBuilder.appendWhere(UserInfo._ID + " = "+ uri.getPathSegments().get(1));
Cursor c = sqlBuilder.query(demoDB, projection, selection,selectionArgs, null, null, sortOrder);
c.setNotificationUri(getContext().getContentResolver(), uri);
Update - update()
int count = 0;
switch (uriMatcher.match(uri)) {
case UserInfo.USERINFO:
count = demoDB.update(UserInfo.DATABASE_TABLE, values, selection,selectionArgs);
break;
case UserInfo.USERINFO_ID:
count = demoDB.update(UserInfo.DATABASE_TABLE, values, UserInfo._ID+ " = "+ uri.getPathSegments().get(1)
+ (!TextUtils.isEmpty(selection) ? " AND (" + selection+ ')' : ""), selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
Delete - delete()
int count = 0;
switch (uriMatcher.match(uri)) {
case UserInfo.USERINFO:
count = demoDB.delete(UserInfo.DATABASE_TABLE, selection,selectionArgs);
break;
case UserInfo.USERINFO_ID:
String id = uri.getPathSegments().get(1);
count = demoDB.delete(UserInfo.DATABASE_TABLE, UserInfo._ID+ " = "+ id+ (!TextUtils.isEmpty(selection) ? " AND (" + selection+ ')' : ""), selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
Create a Database Helper to Create database Schema
Refer : http://about-android.blogspot.com/2009/12/android-hello-world-activity-sample.html
Accessing Our Content Provider
Create or Insert
ContentValues values = new ContentValues();
values.put(UserInfo.isactive, "Y");
Uri uri = getContentResolver().insert(MyContentProvider.CONTENT_URI, values);
Retrieve
String resultStr = "";
Uri allTitles = Uri.parse("content://"+ MyContentProvider.PROVIDER_NAME + "/"+ UserInfo.DATABASE_TABLE);
Cursor c = managedQuery(allTitles, null, null, null, "");
if (c.moveToFirst()) {
do {
resultStr = c.getString(c.getColumnIndex(UserInfo._ID))+ ", "+ c.getString(c.getColumnIndex(UserInfo.isactive));
Toast.makeText(MainActivity.this, resultStr,Toast.LENGTH_LONG).show();
} while (c.moveToNext());
}
Using the onCreate Method we can initilze the Database
@Override
public boolean onCreate() {
Context context = getContext();
SQLiteConnectionManager dbHelper = new SQLiteConnectionManager(context);
demoDB = dbHelper.getWritableDatabase();
return (demoDB == null) ? false : true;
}
getType method is used to validate the input url
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)) {
case UserInfo.USERINFO:
return UserInfo.CONTENT_TYPE;
case UserInfo.USERINFO_ID:
return UserInfo.CONTENT_ITEM_TYPE;
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
}
CRUD Operation
Create - insert()
long rowID = demoDB.insert(UserInfo.DATABASE_TABLE, "", values);
if (rowID > 0) {
Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
getContext().getContentResolver().notifyChange(_uri, null);
return _uri;
}
Retrive - query()
SQLiteQueryBuilder sqlBuilder = new SQLiteQueryBuilder();
sqlBuilder.setTables(UserInfo.DATABASE_TABLE);
if (uriMatcher.match(uri) == UserInfo.USERINFO_ID)
sqlBuilder.appendWhere(UserInfo._ID + " = "+ uri.getPathSegments().get(1));
Cursor c = sqlBuilder.query(demoDB, projection, selection,selectionArgs, null, null, sortOrder);
c.setNotificationUri(getContext().getContentResolver(), uri);
Update - update()
int count = 0;
switch (uriMatcher.match(uri)) {
case UserInfo.USERINFO:
count = demoDB.update(UserInfo.DATABASE_TABLE, values, selection,selectionArgs);
break;
case UserInfo.USERINFO_ID:
count = demoDB.update(UserInfo.DATABASE_TABLE, values, UserInfo._ID+ " = "+ uri.getPathSegments().get(1)
+ (!TextUtils.isEmpty(selection) ? " AND (" + selection+ ')' : ""), selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
Delete - delete()
int count = 0;
switch (uriMatcher.match(uri)) {
case UserInfo.USERINFO:
count = demoDB.delete(UserInfo.DATABASE_TABLE, selection,selectionArgs);
break;
case UserInfo.USERINFO_ID:
String id = uri.getPathSegments().get(1);
count = demoDB.delete(UserInfo.DATABASE_TABLE, UserInfo._ID+ " = "+ id+ (!TextUtils.isEmpty(selection) ? " AND (" + selection+ ')' : ""), selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
Create a Database Helper to Create database Schema
Refer : http://about-android.blogspot.com/2009/12/android-hello-world-activity-sample.html
Accessing Our Content Provider
Create or Insert
ContentValues values = new ContentValues();
values.put(UserInfo.isactive, "Y");
Uri uri = getContentResolver().insert(MyContentProvider.CONTENT_URI, values);
Retrieve
String resultStr = "";
Uri allTitles = Uri.parse("content://"+ MyContentProvider.PROVIDER_NAME + "/"+ UserInfo.DATABASE_TABLE);
Cursor c = managedQuery(allTitles, null, null, null, "");
if (c.moveToFirst()) {
do {
resultStr = c.getString(c.getColumnIndex(UserInfo._ID))+ ", "+ c.getString(c.getColumnIndex(UserInfo.isactive));
Toast.makeText(MainActivity.this, resultStr,Toast.LENGTH_LONG).show();
} while (c.moveToNext());
}