public class StudentProvider extends ContentProvider {
private final static UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
private DBHelper dbhelper = null;
private static int STUDENT = 1;
private static int STUDENTS = 2;
static{
MATCHER.addURI("com.matrix.provides.studentprovider", "student", STUDENTS);
MATCHER.addURI("com.matrix.provides.studentprovider", "student/#", STUDENT);
}
@Override
public boolean onCreate() {
dbhelper = new DBHelper(getContext());
return true;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbhelper.getWritableDatabase();
int rowNo = 0;
switch (MATCHER.match(uri)) {
case 1:
long studnentId = ContentUris.parseId(uri);
String where = "studentId = "+ studnentId;
if(selection != null && !selection.equals("")){
where += " and " + selection;
}
rowNo = db.delete("student", where, selectionArgs);
break;
case 2:
rowNo = db.delete("student", selection, selectionArgs);
break;
default:
throw new IllegalArgumentException("your uri:"+uri.toString()+" is wrong!");
}
return rowNo;
}
@Override
public String getType(Uri uri) {
String result = "";
switch (MATCHER.match(uri)) {
case 1:
result = "vnd.android.cursor.item/student";
break;
case 2:
result = "vnd.android.cursor.dir/student";
break;
}
return result;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
Uri insertUri = null;
switch (MATCHER.match(uri)) {
case 2:
SQLiteDatabase db = dbhelper.getWritableDatabase();
long studentId = db.insert("student", "name", values);
insertUri = ContentUris.withAppendedId(uri, studentId);
break;
default:
throw new IllegalArgumentException("your uri:"+uri.toString()+" is wrong!");
}
return insertUri;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = dbhelper.getReadableDatabase();
Cursor cursor = null;
switch (MATCHER.match(uri)) {
case 1:
long studnentId = ContentUris.parseId(uri);
String where = "studnetId = "+ studnentId;
if(selection != null && !selection.equals("")){
where += " and " + selection;
}
cursor = db.query("student", projection, where, selectionArgs, null, null, sortOrder);
break;
case 2:
cursor = db.query("student", projection, selection, selectionArgs, null, null, sortOrder);
break;
default:
break;
}
return cursor;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbhelper.getWritableDatabase();
int rowNo = 0;
switch (MATCHER.match(uri)) {
case 1:
long studnentId = ContentUris.parseId(uri);
String where = "studentId = "+ studnentId;
if(selection != null && !selection.equals("")){
where += " and " + selection;
}
rowNo = db.update("student", values, where, selectionArgs);
break;
case 2:
rowNo = db.update("student", values, selection, selectionArgs);
break;
default:
throw new IllegalArgumentException("your uri:"+uri.toString()+" is wrong!");
}
return rowNo;
}
}
<provider android:name="com.matrix.androidstudy.StudentProvider" android:authorities="com.matrix.provides.studentprovider"></provider>
public class Test extends AndroidTestCase {
public void testInsert(){
Uri uri = Uri.parse("content://com.matrix.provides.studentprovider/student");
ContentResolver resolver = getContext().getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "like");
values.put("age", 23);
resolver.insert(uri, values);
}
public void testDelete(){
Uri uri = Uri.parse("content://com.matrix.provides.studentprovider/student/1");
ContentResolver resolver = getContext().getContentResolver();
resolver.delete(uri, null, null);
}
public void testUpdate(){
Uri uri = Uri.parse("content://com.matrix.provides.studentprovider/student/1");
ContentResolver resolver = getContext().getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "good man");
resolver.update(uri, values, null, null);
}
public void testQuery(){
Uri uri = Uri.parse("content://com.matrix.provides.studentprovider/student");
ContentResolver resolver = getContext().getContentResolver();
Cursor cursor = resolver.query(uri, null, null, null, "studentId asc");
while(cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
int id = cursor.getInt(cursor.getColumnIndex("studentId"));
Log.i("student", id+" "+name);
}
}
}