您的位置:首页 > 编程语言 > PHP开发

ContentProvider 实例

2014-05-11 10:23 225 查看
这个实例是建立在上一个sqlite数据库上的, sqlite中提供ContentProvider, Other项目写测试方法来访问:

需要注意的是, ContentProvider需要在AndroidMainifest.xml 中注册:

package com.xiaoming.sqlite;

import java.util.regex.Matcher;

import com.xiaoming.domain.DBOpenHelper;

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 PersonsProvider extends ContentProvider {

	DBOpenHelper  dbOpenHelper = null;
	public static final String     AUTHORITIES =  "com.xiaoming.sqlite.provicer";
	public static final UriMatcher Matcher = new UriMatcher(UriMatcher.NO_MATCH);
	public static final int        PERSONS =  1;
	public static final int        PERSON =  2;
	
	static {
		Matcher.addURI(AUTHORITIES, "persons",PERSONS );
		Matcher.addURI(AUTHORITIES, "persons/#",PERSON ); //#代表数字, *代表所有字符
	}
	
	@Override
	public boolean onCreate() {
		dbOpenHelper = new DBOpenHelper(this.getContext());
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
		
		switch (Matcher.match(uri)) {
		case 1:
			return db.query("persons", projection, selection, selectionArgs, null, null, sortOrder);
		case 2:
			long rowid = ContentUris.parseId(uri);
			String where = "_id="+rowid;
			if( selection != null && !"".equals(selection)){
				where += " and "+selection;
			}
			return db.query("persons", projection, where, selectionArgs, null, null, sortOrder);

		default:
			throw new IllegalArgumentException("this is a unknow uri:"+uri);
		}
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		switch (Matcher.match(uri)) {
		case 1:
			long rowid = db.insert("persons", "name", values);
			Uri insertUri = Uri.parse(AUTHORITIES+"/persons/"+rowid);
			//Uri insertUri = ContentUris.withAppendedId(uri, rowid);
			return insertUri;

		default:
			throw new IllegalArgumentException("this is Unknown Uri:"+uri);
		}
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		int num = 0;
		switch (Matcher.match(uri)) {
		case 1:
			num = db.delete("persons", selection, selectionArgs);
			break;
		case 2:
			long rowid = ContentUris.parseId(uri);
			String where = "_id="+rowid;
			if( selection!=null && !"".equals(selection.trim()) )
			{
				where += " and " + selection;
			}
			num = db.delete("persons", where, selectionArgs);
			break;

		default:
			throw new IllegalArgumentException("this is Unknown Uri:"+uri);
		}
		return num;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		int num = 0;
		switch (Matcher.match(uri)) {
		case 1:
			num = db.update("persons", values, selection, selectionArgs);
			break;
		case 2:
			long rowid = ContentUris.parseId(uri);
			String where = "_id="+rowid;
			if( selection!=null && !"".equals(selection.trim()) )
			{
				where += " and " + selection;
			}
			num = db.update("persons", values, where, selectionArgs);
			break;

		default:
			throw new IllegalArgumentException("this is Unknown Uri:"+uri);
		}
		return num;
	}

}


Other项目中的测试代码:
package com.xiaoming.other.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 ContentProviderTest extends AndroidTestCase {

	
	private static final String TAG = "ContentProviderTest";

	public void testContentProvicerTest() throws Exception{
		Uri uri = Uri.parse("content://com.xiaoming.sqlite.provicer/persons");
		ContentResolver resolver = this.getContext().getContentResolver();
		ContentValues values = new ContentValues();
		values.put("name", "android");
		values.put("age", 1000);
		resolver.insert(uri, values);
		
	}
	
	public void testDelete() throws Exception
	{
		Uri uri = Uri.parse("content://com.xiaoming.sqlite.provicer/persons/7");
		ContentResolver resolver = this.getContext().getContentResolver();
		resolver.delete(uri, null , null);
	}
	
	public void testUpdate() throws Exception
	{
		Uri uri = Uri.parse("content://com.xiaoming.sqlite.provicer/persons/9");
		ContentValues values = new ContentValues();
		values.put("name", "小黄");
		ContentResolver resolver = this.getContext().getContentResolver();
		resolver.update(uri, values, null, null);
	}
	
	public void testQuery() throws Exception
	{
		Uri uri = Uri.parse("content://com.xiaoming.sqlite.provicer/persons");
		ContentResolver resolver = this.getContext().getContentResolver();
		Cursor  result = resolver.query(uri, null, null, null, null);
		while( result.moveToNext() )
		{
			Log.i(TAG,result.getString(result.getColumnIndex("name") ));
		}
	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: