這一節詳細介紹一下Model層的設計,本身並無太多痛點,採用標準的Provider結構訪問底層資料庫。簡單UML圖如下:
通過ThemeProvider統一訪問資料庫具體實現ThemeDBHelper。通過向ThemeProvider添加相應的Observer來監聽資料庫的變化。這裡屬於標準的Provider操作、及sqlite操作,由於不涉及到多個應用資料共用的操作,只是使用Provider接管、簡化對資料庫的訪問操作,所以實現相對簡單,唯一需要注意的是由於需要Observer來監聽資料庫的變化,所以在Provider的相關操作後,需要通過sendNotification來通知相關監聽器。
import android.content.ContentProvider;import android.content.ContentUris;import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteQueryBuilder;import android.net.Uri;import android.text.TextUtils;import android.util.Log;public class ThemeProvider extends ContentProvider { private final static String TAG = ThemeProvider.class.toString();public static final String AUTHORITY = "com.tencent.themedatabase";// 對外提供服務介面名public static final String NOTIFICATION = "notify";// 是否通知提示private static ThemeDBHelper mThemeDBHelper;private Context mContext;//public ThemeProvider(Context context) {//mContext = context;//mThemeDBHelper = new ThemeDBHelper(context);////}private long checkandInsert(SQLiteDatabase db, ContentValues cv,String table, String nullColumnHack) {if (cv.get(ThemeDBHelper._ID) == null) {throw new RuntimeException("Error : the Insert value has no id");}return db.insert(table, nullColumnHack, cv);}private void sendNotify(Uri uri) {String notify = uri.getQueryParameter(NOTIFICATION);if (notify == null || notify.equals("true")) {mContext.getContentResolver().notifyChange(uri, null);}}public static long generateNewId() {return mThemeDBHelper.generateNewId();}@Overridepublic int delete(Uri uri, String selection, String[] selectionArgs) {/**/SqlArguments sqlargs = new SqlArguments(uri, selection, selectionArgs);SQLiteDatabase db = mThemeDBHelper.getWritableDatabase();int count = db.delete(sqlargs.table, sqlargs.where, sqlargs.selection);if (count > 0) {sendNotify(uri);}return 0;}@Overridepublic String getType(Uri uri) {SqlArguments sqlarg = new SqlArguments(uri);if (TextUtils.isEmpty(sqlarg.where)) {return "vnd.android.cursor.dir/" + sqlarg.table;} else {return "vnd.android.cursor.item/" + sqlarg.table;}}@Overridepublic Uri insert(Uri uri, ContentValues values) {SqlArguments sqlargs = new SqlArguments(uri);SQLiteDatabase db = mThemeDBHelper.getWritableDatabase();long id = checkandInsert(db, values, sqlargs.table, null);if (id < 0) {return null;}uri = ContentUris.withAppendedId(uri, id);sendNotify(uri);return uri;}@Overridepublic int bulkInsert(Uri uri, ContentValues[] values) {SqlArguments sqlargs = new SqlArguments(uri);SQLiteDatabase db = mThemeDBHelper.getWritableDatabase();db.beginTransaction();try {for(int i=0; i<values.length;i++){if(checkandInsert(db, values[i],sqlargs.table, null) <0){return 0;}}db.setTransactionSuccessful();} catch (Exception e) {e.printStackTrace();}finally{db.endTransaction();}sendNotify(uri);return values.length;}@Overridepublic boolean onCreate() {mContext = this.getContext();mThemeDBHelper = new ThemeDBHelper(mContext);return true;}@Overridepublic Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) {SqlArguments sqlargs = new SqlArguments(uri, selection, selectionArgs);SQLiteQueryBuilder qb = new SQLiteQueryBuilder();qb.setTables(sqlargs.table);SQLiteDatabase db = mThemeDBHelper.getReadableDatabase();Cursor cr = qb.query(db, projection, sqlargs.where, sqlargs.selection,null, null, sortOrder);cr.setNotificationUri(mContext.getContentResolver(), uri);return cr;}@Overridepublic int update(Uri uri, ContentValues values, String selection,String[] selectionArgs) {SqlArguments sqlargs = new SqlArguments(uri, selection, selectionArgs);SQLiteDatabase db = mThemeDBHelper.getWritableDatabase();int count = db.update(sqlargs.table, values, sqlargs.where,sqlargs.selection);Log.e(TAG,"GET WHERE "+sqlargs.where);if (count > 0) {sendNotify(uri);}return count;}static class SqlArguments {/** * 處理sql語句,解析各個參數 * */public final String table;public final String where;public final String[] selection;public SqlArguments(Uri uri, String where, String[] selection) {int argscount = uri.getPathSegments().size();if (argscount == 1) {// uri://host/tablethis.table = uri.getPathSegments().get(0);Log.e("pluszhang","parse uri "+this.table);this.where = where;this.selection = selection;} else if (argscount != 2) {// uri://host/table/id/...throw new IllegalArgumentException("Invalidate URI " + uri);} else if (!TextUtils.isEmpty(where)) {// uri://host/talbe/id,where應該為空白,已經指明idthrow new UnsupportedOperationException("WHERE opt not support " + uri);} else {// uri://host/table/idthis.table = uri.getPathSegments().get(0);this.where = "_id=" + uri.getPathSegments().get(1);this.selection = null;}}public SqlArguments(Uri uri) {if (uri.getPathSegments().size() == 1) {this.table = uri.getPathSegments().get(0);this.where = null;this.selection = null;} else {throw new IllegalArgumentException("Invalidate URI " + uri);}}}}
——歡迎轉載,請註明出處http://blog.csdn.net/zyplus——