Android ContentProvider实例

URI介绍:


AndroidManifest.xml

在<application>节点下加入

<provider android:name=".PersonProvider" android:authorities="com.lipeng.providers.personprovider" android:exported="true"/>

PersonProvider.java

package com.example.test_sqlite;

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;

import com.example.test_sqlite.service.SQLHelper;

public class PersonProvider extends ContentProvider{

	private SQLHelper sqlHelper = null;
	private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);	// NO_MATCH值为-1
	private static final int PERSONS = 1;
	private static final int PERSON = 2;
	private final String TABLE_NAME_PERSON = "person";
	
	static{
		MATCHER.addURI("com.lipeng.providers.personprovider", "person", PERSONS);
		MATCHER.addURI("com.lipeng.providers.personprovider", "person/#", PERSON);	
	}
	
	@Override
	public boolean onCreate() {
		sqlHelper = new SQLHelper(getContext());
		return true;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		SQLiteDatabase db = sqlHelper.getWritableDatabase();
		switch (MATCHER.match(uri)) {
		case PERSONS:
			long rowid = db.insert(TABLE_NAME_PERSON, "name", values); // 主键值
			Uri insertUri = ContentUris.withAppendedId(uri, rowid);
			return insertUri;
		default:
			throw new IllegalArgumentException("this is Unknow Uri:" + uri);
		}
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		SQLiteDatabase db = sqlHelper.getWritableDatabase();
		int num = 0;
		switch (MATCHER.match(uri)) {
		case PERSONS:
			num = db.delete(TABLE_NAME_PERSON, selection, selectionArgs);
			break;
		case PERSON:
			long rowid = ContentUris.parseId(uri);
			String where = "personid=" + rowid;
			if(selection != null && !"".equals(selection.trim())){
				where += " and " + selection;
			}
			num = db.delete(TABLE_NAME_PERSON, where, selectionArgs);
			break;
		default:
			throw new IllegalArgumentException("this is Unknow Uri:" + uri);
		}
		return num;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		SQLiteDatabase db = sqlHelper.getWritableDatabase();
		switch (MATCHER.match(uri)) {
		case PERSONS:
			return db.update(TABLE_NAME_PERSON, values, selection, selectionArgs);
		case PERSON:
			long rowid = ContentUris.parseId(uri);
			String where = "personid=" + rowid;
			if(selection != null && !"".equals(selection.trim())){
				where += " and " + selection;
			}
			return db.update(TABLE_NAME_PERSON, values, where, selectionArgs);
		default:
			throw new IllegalArgumentException("this is Unknow Uri:" + uri);
		}
	}

	/**
	 * projection 要查询的字段
	 */
	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		// 约定:/person表示查询表的所有数据;/person/10表示查询id为10的数据
		SQLiteDatabase db = sqlHelper.getReadableDatabase();
		switch (MATCHER.match(uri)) {
		case PERSONS:
			return db.query(TABLE_NAME_PERSON, projection, selection, selectionArgs, null, null, sortOrder);
		case PERSON:
			long rowid = ContentUris.parseId(uri);
			String where = "personid=" + rowid;
			if(selection != null && !"".equals(selection.trim())){
				where += " and " + selection;
			}
			return db.query(TABLE_NAME_PERSON, null, where, selectionArgs, null, null, sortOrder);
		default:
			throw new IllegalArgumentException("this is Unknow Uri:" + uri);
		}
	}

	@Override
	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("this is Unknow Uri:" + uri);
		}
	}
}


单元测试类

AccessContentProvider.java

package com.example.test;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.test.AndroidTestCase;
import android.util.Log;

public class AccessContentProvider extends AndroidTestCase{
	private static final String TAG = "AccessContentProvider"; 

	public void testInsert(){
		String uriStr = "content://com.lipeng.providers.personprovider/person";
		Uri uri = Uri.parse(uriStr);
		ContentResolver resolver = getContext().getContentResolver();
		ContentValues values = new ContentValues();
		values.put("name", "xxxxxxxxxxxxxx");
		values.put("phone", "1311212313");
		values.put("amount", "123131");
		resolver.insert(uri, values);
	} 
	
	public void testDelete(){
		String uriStr = "content://com.lipeng.providers.personprovider/person/80";
		Uri uri = Uri.parse(uriStr);
		ContentResolver resolver = getContext().getContentResolver();
		
		// 这里不能再制定personid=?,因为最终会组成例如personid=10 and personid=20
		resolver.delete(uri, "name=?", new String[]{"lipeng"});
		// 如果什么都不需要指定,只需要设置/person/20,而delete函数后两个参数为null就行
//		resolver.delete(uri, null, null);
	} 
	
	public void testUpdate(){
		String uriStr = "content://com.lipeng.providers.personprovider/person/79";
		Uri uri = Uri.parse(uriStr);
		ContentResolver resolver = getContext().getContentResolver();
		ContentValues values = new ContentValues();
		values.put("name", "John");
		values.put("phone", "131565464532");
		values.put("amount", "90000000");
		resolver.update(uri, values, null, null);
	} 
	
	public void testQuery(){
		String uriStr = "content://com.lipeng.providers.personprovider/person/70";	// 查询id为70的Person
		String uriStr2 = "content://com.lipeng.providers.personprovider/person";	// 查询所有Person
		Uri uri = Uri.parse(uriStr2);
		ContentResolver resolver = getContext().getContentResolver();
		Cursor cursor = resolver.query(uri, null, null, null, "personid asc");
		while(cursor.moveToNext()){
			String name = cursor.getString(cursor.getColumnIndex("name"));
			Log.i(TAG, name);
		}
	} 
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值