ContentProvider 实例
来源:互联网 发布:mac windows10 iso 编辑:程序博客网 时间:2024/05/29 05:54
这个实例是建立在上一个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 ); //#代表数字, *代表所有字符}@Overridepublic boolean onCreate() {dbOpenHelper = new DBOpenHelper(this.getContext());return true;}@Overridepublic 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);}}@Overridepublic String getType(Uri uri) {// TODO Auto-generated method stubreturn null;}@Overridepublic 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);}}@Overridepublic 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;}@Overridepublic 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;}}
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") ));}}}
0 0
- ContentProvider实例
- ContentProvider实例
- ContentProvider 实例
- ContentProvider实例
- ContentProvider实例
- MyNotes(ContentProvider实例)
- 自定义ContentProvider 实例演示
- ContentProvider原理及实例
- contentprovider讲解+实例
- (Android) ContentProvider 实例
- 操纵系统ContentProvider实例
- ContentProvider与ContentResolver实例
- ContentProvider实例整理
- android ContentProvider 使用实例
- ContentProvider原理及实例 .
- ContentProvider原理及实例
- ContentProvider原理及实例
- Android ContentProvider实例
- 2013级C++第12周(春)项目——成员的访问属性、多重继承
- 第四周作业
- QGIS编译
- 多益机试题目--字符串匹配的变形
- [LeetCode]Reverse Words in a String
- ContentProvider 实例
- 《漫画线性代数》读书笔记 序
- 昨天是母亲节,祝母亲天天开心,永远健康
- 2-05. 求集合数据的均方差(15)
- fedora(cent os) +Apache+PHP+MySQL服务器环境
- 走在最前端的转换软件
- Oracle的For Update语句
- Linux环境编程之文件I/O(四):文件I/O的数据结构
- POJ 1149 PIGS 网络流Dinic