Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android開發——數據存儲之ContentProvider(2)

Android開發——數據存儲之ContentProvider(2)

編輯:Android開發實例

2、創建自定義ContentProvider 創建內容供應器,需要繼承ContentProvider   示例代碼三:
   1:  
   2: /**
   3:  * 內容提供者
   4:  * @author 張漢國
   5:  */
   6: public class TodoListProvider extends ContentProvider {
   7:  
   8:     private static final String TAG = TodoListProvider.class.getSimpleName();
   9:  
  10:     //數據庫名
  11:     private static final String DATABASE_NAME = "todolist.db";
  12:     //版本
  13:     private static final int DATABASE_VERSION = 1;
  14:     //表名
  15:     private static final String TABLE_NAME = "todos";
  16:     //屬性表
  17:     private static HashMap<String, String> sTodosProjectionMap;
  18:  
  19:     private static final int TODOS = 1;
  20:     private static final int TODO_ID = 2;
  21:  
  22:     //uri匹配器
  23:     private static final UriMatcher uriMatcher;
  24:  
  25:     private DataBaseHelper dataBaseHelper;
  26:  
  27:     static {
  28:         //數據初始化
  29:         uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
  30:         uriMatcher.addURI(TodoList.AUTHORITY, "todos", TODOS);
  31:         uriMatcher.addURI(TodoList.AUTHORITY, "todos/#", TODO_ID);
  32:  
  33:         sTodosProjectionMap = new HashMap<String, String>();
  34:         sTodosProjectionMap.put(Todo._ID, Todo._ID);
  35:         sTodosProjectionMap.put(Todo.TITLE, Todo.TITLE);
  36:         sTodosProjectionMap.put(Todo.CONTENT, Todo.CONTENT);
  37:         sTodosProjectionMap.put(Todo.CREATED_DATE, Todo.CREATED_DATE);
  38:         sTodosProjectionMap.put(Todo.MODIFIED_DATE, Todo.MODIFIED_DATE);
  39:     }
  40:  
  41:     private static class DataBaseHelper extends SQLiteOpenHelper {
  42:  
  43:         public DataBaseHelper(Context context) {
  44:             super(context, DATABASE_NAME, null, DATABASE_VERSION);
  45:         }
  46:  
  47:         //創建表
  48:         @Override
  49:         public void onCreate(SQLiteDatabase db) {
  50:             Log.d(TAG, "create table :"   TABLE_NAME);
  51:             db.execSQL("CREATE TABLE IF NOT EXISTS "   TABLE_NAME   "("   Todo._ID   " INTEGER PRIMARY KEY,"   Todo.TITLE
  52:                       " TEXT,"   Todo.CONTENT   " TEXT,"   Todo.CREATED_DATE   " INTEGER,"   Todo.MODIFIED_DATE
  53:                       " INTEGER)");
  54:             ContentValues values = new ContentValues();
  55:             values.put(Todo.TITLE, "test");
  56:             values.put(Todo.CONTENT, "dddddddd");
  57:             db.insert(TABLE_NAME, "title", values);
  58:         }
  59:  
  60:         //更新數據庫表
  61:         @Override
  62:         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  63:             Log.w(TAG, "Upgrading database from version "   oldVersion   " to "   newVersion
  64:                       ", which will destroy all old data");
  65:             db.execSQL("DROP TABLE IF EXISTS "   TABLE_NAME);
  66:             onCreate(db);
  67:         }
  68:  
  69:     }
  70:  
  71:     //刪除數據操作
  72:     @Override
  73:     public int delete(Uri uri, String selection, String[] selectionArgs) {
  74:         SQLiteDatabase db = dataBaseHelper.getWritableDatabase();
  75:         int count = 0;
  76:         //判斷uri類型
  77:         switch (uriMatcher.match(uri)) {
  78:         case TODOS:
  79:             //如果以"/todo"結尾,則刪除整個表數據
  80:             count = db.delete(TABLE_NAME, selection, selectionArgs);
  81:             break;
  82:         case TODO_ID:
  83:             //如果以“/todo/1”結尾,則刪除ID=1的數據
  84:             String todoId = uri.getPathSegments().get(1);
  85:             count = db.delete(TABLE_NAME, Todo._ID   "="   todoId
  86:                       ((TextUtils.isEmpty(selection)) ? "" : " AND "   selection), selectionArgs);
  87:             break;
  88:         default:
  89:             throw new IllegalArgumentException("Unknown URI :"   uri);
  90:         }
  91:         getContext().getContentResolver().notifyChange(uri, null);
  92:         return count;
  93:     }
  94:     ......
  95:     ......
  96:     //其他方法省略

至此我們就創建了一個ContentProvider了

3、Provider的配置,在AndroidMainfest.xml配置

示例代碼三:

   1: <?xml version="1.0" encoding="utf-8"?>
   2: <manifest xmlns:android="http://schemas.android.com/apk/res/android"
   3:     package="com.halzhang.android.provider" android:versionCode="1"
   4:     android:versionName="1.0">
   5:     <application android:icon="@drawable/icon" android:label="@string/app_name">
   6:         <!-- 
   7:     name屬性為繼承ContentProvider的類
   8:     authorities屬性為屬性對象路徑
   9:      -->
  10:         <provider android:name=".TodoListProvider"
  11:             android:authorities="com.halzhang.android.provider.TodoList"></provider>
  12:         <activity android:name=".ContentProviderDemo" android:label="@string/app_name">

 

OK,至此我們就創建了一個ContentProvider了。

轉自:http://www.cnblogs.com/halzhang/archive/2010/05/28/1746589.html

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved