Android应用开发之ContentProvider

package cn.class3g.db;       import cn.class3g.service.DatabaseHelper;   import Android.content.ContentProvider;   import android.content.ContentUris;   import android.content.ContentValues;   import android.content.UriMatcher;   import android.database.Cursor;   import android.database.sqlite.SQLiteDatabase;   import android.net.Uri;       public class PersonProvider extends ContentProvider {          private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);              private static final int PERSONS = 1;          private static final int PERSON = 2;              private DatabaseHelper dbHelper;              static {                 matcher.addURI("cn.class3g.providers.personprovider", "person", PERSONS);                 matcher.addURI("cn.class3g.providers.personprovider", "person/#",                               PERSON);          }              public boolean onCreate() {                 dbHelper = new DatabaseHelper(this.getContext());                 return true;          }              //  content://cn.itcast.provides.personprovider/person          public Uri insert(Uri uri, ContentValues values) {                 SQLiteDatabase db = dbHelper.getWritableDatabase();                 long rowId;                     switch (matcher.match(uri)) {                 case PERSONS: //向表中添加新纪录并返回其行号                        rowId = db.insert("person", "personid", values);                        return ContentUris.withAppendedId(uri, rowId);                 default:                        throw new IllegalArgumentException("Unknow Uri:" + uri);                 }          }              public Cursor query(Uri uri, String[] projection, String selection,                        String[] selectionArgs, String sortOrder) {                 SQLiteDatabase db = dbHelper.getReadableDatabase();                 switch (matcher.match(uri)) {                 case PERSONS:                        return db.query("person", projection, selection, selectionArgs, null, null, sortOrder);                 case PERSON:                        long personid = ContentUris.parseId(uri);                        String where = "personid="+ personid;                        if(selection!=null && !"".equals(selection)){                               wherewhere = where + " and "+ selection;                        }                        return db.query("person", projection, where, selectionArgs, null, null, sortOrder);                                        default:                        throw new IllegalArgumentException("Unknown Uri:"+ uri);                 }          }              //  content://cn.itcast.provides.personprovider/person 更新表中的所有记录          //  content://cn.itcast.provides.personprovider/person/10 更新表中指定id的记录          public int update(Uri uri, ContentValues values, String selection,                        String[] selectionArgs) {                 SQLiteDatabase db = dbHelper.getWritableDatabase();                 int num;                 switch(matcher.match(uri)){                 case PERSONS: //更新指定记录                        num = db.update("person", values, selection, selectionArgs);                        break;                 case PERSON:                        long personid = ContentUris.parseId(uri);                        String where = "personid=" + personid;                        if(selection != null){                               where += " and " + selection;                        }                        num = db.update("person", values, where, selectionArgs);                        break;                 default:                        throw new IllegalArgumentException("Unknow Uri"+uri);                 }                 return num;          }              public int delete(Uri uri, String selection, String[] selectionArgs) {                 SQLiteDatabase db = dbHelper.getWritableDatabase();                 int num = 0;                 switch (matcher.match(uri)) {                 case PERSONS:                        num = db.delete("person", selection, selectionArgs);                        break;                 case PERSON:                        long personid = ContentUris.parseId(uri);                        String where = "personid="+ personid;                        if(selection!=null && !"".equals(selection)){                               wherewhere = where + " and "+ selection;                        }                        num = db.delete("person", where, selectionArgs);                        break;                     default:                        throw new IllegalArgumentException("Unknown Uri:"+ uri);                 }                 return num;          }              public String getType(Uri uri) {                 switch (matcher.match(uri)) {                 case PERSONS:                        return "vnd.android.cursor.dir/person";                 case PERSON:                        return "vnd.android.cursor.item/person";                 default:                        throw new IllegalArgumentException("Unknown Uri:"+ uri);                 }          }   }  

2、配置

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wyjpdx.html