Android应用开发之ContentProvider
2012-05-23 14:04
369 查看
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)){ where = 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)){ where = 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); } } }
相关文章推荐
- Android应用开发揭秘的第3个程序3_2_Content_Provider修改版和高仿版的源码注释
- Android应用开发之ContentProvider
- Android开发教程之ContentProvider数据存储
- Android开发--详解ContentProvider/Cursor的使用
- [android开发实例] sqlite+content provider+cursor adapter的最简单实现示例
- android ContentProvider读取其他应用数据库
- android开发位置应用getBestProvider返回为空的问题
- Android开发--详解ContentProvider/Cursor的使用
- 24、Android应用-找一个朋友 (整合SQLite/Content Provider/Google Maps)
- android异步查询 sqlitehelper contentprovider loader
- Android(安卓)开发中可能用到的应用发布平台
- android 蓝牙锁应用实例开发(一) 简介
- Android应用开发SharedPreferences存储数据的使用方法
- Android应用开发中使用deeplink
- 在开发第一个Android应用之前需要知道的5件事:
- Window下使用android NDK开发Android应用-入门篇
- Android 上的 10 款 Web 开发应用工具
- 【Android 应用开发】Activity生命周期 与 Activity 之间的通信
- Android应用开发提高篇(4)-----Socket编程(多线程、双向通信)
- [Android应用开发] 02.界面展现和文件权限