android基础--内容提供者

 

public class PersonProvider extends ContentProvider {

    private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);

    private static final int PERSON = 1;

    private static final int PERSON2 = 2;

    static {

       MATCHER.addURI("cn.com.providers.personprovider", "person", PERSON);

       MATCHER.addURI("cn.com.providers.personprovider", "person/#", PERSON2);

    }

    private DBOpenHelper dbOpenHelper=null;

    //删除

    public int delete(Uri uri, String selection, String[] selectionArgs) {

       SQLiteDatabase db = dbOpenHelper.getWritableDatabase();

       int num = 0;

       switch (MATCHER.match(uri)) {

       case PERSON:

           num = db.delete("person", selection, selectionArgs);

           break;

       case PERSON2:

           long id = ContentUris.parseId(uri);

           String where = " personid = "+id;

           if(selection!=null&&!"".equals(selection.trim())){

              where = where + " and "+selection;

           }

           db.delete("person", where, selectionArgs);

           break;

       default:

           throw new IllegalArgumentException("uri 不合法");

       }

       return num;

    }

   

    public String getType(Uri uri) {

       switch (MATCHER.match(uri)) {

       case PERSON:

           return "vnd.android.cursor.dir/person";

       case PERSON2:

           return "vnd.android.cursor.item/person";

       default:

           throw new IllegalArgumentException("uri err!");

       }

    }

   

    //增加

    public Uri insert(Uri uri, ContentValues values) {

       SQLiteDatabase db = dbOpenHelper.getWritableDatabase();

       switch (MATCHER.match(uri)) {

       case PERSON:

           long rowid = db.insert("person", "name", values);

           Uri insertUri = ContentUris.withAppendedId(uri, rowid);

           return insertUri;

       default:

           throw new IllegalArgumentException("uri 不合法");

       }

    }

   

    public boolean onCreate() {

       dbOpenHelper = new DBOpenHelper(getContext());

       return true;

    }

   

    public Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) {

       SQLiteDatabase db = dbOpenHelper.getReadableDatabase();

       switch (MATCHER.match(uri)) {

       case PERSON:

           return db.query("person", projection, selection,selectionArgs, null, null, sortOrder);

       case PERSON2:

           long id = ContentUris.parseId(uri);

           String where = " personid = "+id;

           if(selection!=null&&!"".equals(selection.trim())){

              where = where + " and "+selection;

           }

           return db.query("person", projection, where,selectionArgs, null, null, sortOrder);

       default:

           throw new IllegalArgumentException("uri error@!");

       }

    }

    //更新

    public int update(Uri uri, ContentValues values, String selection,String[] selectionArgs) {

       SQLiteDatabase db = dbOpenHelper.getWritableDatabase();

       int num = 0;

       switch (MATCHER.match(uri)) {

       case PERSON:

           num = db.update("person", values, selection, selectionArgs);

           break;

       case PERSON2:

           long id = ContentUris.parseId(uri);

           String where = " personid = "+id;

           if(selection!=null&&!"".equals(selection.trim())){

              where = where +" and "+selection;

           }

           num =db.update("person", values, where, selectionArgs);

           break;

       default:

           throw new IllegalArgumentException("uri非法");

       }

       return num;

    }

}

package cn.itcast.test;

 

public class AccessContentProviderTest extends AndroidTestCase {

       public void testInsert() throws Throwable{

              ContentResolver resolver = getContext().getContentResolver();

              Uri uri = Uri.parse("content://cn.com.providers.personprovider/person");

              ContentValues values = new ContentValues();

              values.put("name", "lili");

              values.put("phone", "110");

              values.put("amount", "1000000");

              for(int i = 0;i<20;i++){

                     resolver.insert(uri, values);

              }

       }

       public void testDelete()throws Throwable{

              ContentResolver resolver=getContext().getContentResolver();

              Uri uri = Uri.parse("content://cn.com.providers.personprovider/person/403");

              resolver.delete(uri, null, null);

       }

       public void testUpdata()throws Throwable{

              ContentResolver resolver= getContext().getContentResolver();

              Uri uri= Uri.parse("content://cn.com.providers.personprovider/person/404");

              ContentValues values = new ContentValues();

              values.put("amount", 25);

              resolver.update(uri, values, null, null);

       }

       public void testQuery() throws Throwable{

              ContentResolver resolver = getContext().getContentResolver();

              Uri uri = Uri.parse("content://cn.com.providers.personprovider/person");

              Cursor cursor = resolver.query(uri, null, null, null, "personid asc");

              while(cursor.moveToNext()){

                     String name = cursor.getString(cursor.getColumnIndex("name"));

                     Log.i("AccessContentProviderTest", name);

              }

       }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值