ContentProvider是android的四大組件之一,在編寫代碼的時候最好是加上單元測試,這樣能夠肯定對數據的CRUD的正確。本篇文章主要介紹ContentProvider中兩個主要輔助類的使用還有單元測試的在ContentProvider中的使用。java
須要用到的兩個輔助類:UriMatcher類和ContentUris類。android
UriMatcher類:可以對輸入的uri參數就行匹配,以肯定對什麼表執行什麼樣的操做。sql
ContentUris類:有些方法須要返回uri,運用此類能夠方便的生成uri類。app
對於單元測試,我的以爲很是有必要在從此寫代碼的時候使用,這樣能夠很是準確的肯定代碼的正確性。ide
使用單元測試的步驟:單元測試
1)加入instrumentation,這個部分的代碼是固定,也能夠徹底在ADT提供的嚮導中導入。測試
<instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.example.android_contentprovider" > </instrumentation>
2)添加<uses-library>,這個部分的代碼也是固定的寫法。this
<uses-library android:name="android.test.runner" />
好了,必備的知識已經講完了,如今上代碼:spa
1)生成一個SQLiteDatabase類,這個是必需的類MySQLiteOpenHelper類.net
package com.app.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class MySQLiteOpenHelper extends SQLiteOpenHelper { private static String DB_NAME = "test.db3"; private static int VERSION = 1; public MySQLiteOpenHelper(Context context) { super(context, DB_NAME, null, VERSION); } @Override public void onCreate(SQLiteDatabase db) { //建表語句 String create_student = "create table student(_id integer primary key autoincrement,name varchar(10),age integer,gender vachar(10))"; db.execSQL(create_student); //千萬不能執行這句
// db.close(); } @Override public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { } }
而後添加咱們須要的MyContentProvider類:
package com.app.contentprovider; import com.app.db.MySQLiteOpenHelper; 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; import android.util.Log; public class MyContentProvider extends ContentProvider { MySQLiteOpenHelper helper = null; private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); // 匹配單條記錄 private static final int student = 1; // 匹配多條記錄 private static final int students = 2; static { matcher.addURI("com.app.wx", "student/#", student); matcher.addURI("com.app.wx", "student", students); } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { SQLiteDatabase db = helper.getWritableDatabase(); int action = matcher.match(uri); switch (action) { // 匹配單條記錄 case student: long id = ContentUris.parseId(uri); //獲取單條記錄的id號 String delete_id = "_id=" + id; if (selection != null) { delete_id += delete_id + " and " + selection; } db.delete("student", delete_id, selectionArgs); break; // 匹配多條記錄 case students: db.delete("student", selection, selectionArgs); break; } return 0; } //必需實現這個方法,這個方法與intent有關係,之後再講 @Override public String getType(Uri uri) { int code = matcher.match(uri); switch (code) { case student: return "vnd.android.cursor.item/student_item"; case students: return "vnd.android.cursor.dir/students"; default: return null; } } @Override public Uri insert(Uri uri, ContentValues values) { SQLiteDatabase db = helper.getWritableDatabase(); int action = matcher.match(uri); switch (action) { case students: long id1 = db.insert("student", "_id", values); Log.i("--------", ContentUris.withAppendedId(uri, id1).toString()); return ContentUris.withAppendedId(uri, id1); } return null; } @Override public boolean onCreate() { helper = new MySQLiteOpenHelper(this.getContext()); return true; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String orderBy) { SQLiteDatabase db = helper.getWritableDatabase(); Cursor cursor = null; int action = matcher.match(uri); switch (action) { case students: cursor = db.query("student", projection, selection, selectionArgs, null, null, orderBy); break; } System.out.println("-----------count:" + cursor.getCount()); return cursor; } @Override public int update(Uri uri, ContentValues values, String selection, String[] arg3) { int count = -1; SQLiteDatabase db = helper.getWritableDatabase(); int action = matcher.match(uri); switch (action) { case student: // 以id來處理更新 long id = ContentUris.parseId(uri); String id_selection = "_id=" + id; if (selection != null && !selection.equals("")) { id_selection = id_selection + " and " + values; } count = db.update("student", values, id_selection, arg3); System.out.println("----------count:" + count); break; } return count; } }
這個類很長,可是執行的方法都是比較常見的CURD的方法,重要的是UriMatcher和ContentUris類的使用。
接着執行單元測試類:Test
package com.app.contentprovider; 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 Test extends AndroidTestCase { public void insert() { ContentResolver resolver = this.getContext().getContentResolver(); String str = "content://com.app.wx/student"; ContentValues values = new ContentValues(); values.put("name", "wzq"); values.put("age", 18); values.put("gender", "boy"); resolver.insert(Uri.parse(str), values); } public void update() { ContentResolver resolver = this.getContext().getContentResolver(); String str = "content://com.app.wx/student/2"; ContentValues values = new ContentValues(); values.put("name", "哈哈"); resolver.update(Uri.parse(str), values, null, null); } public void query() { ContentResolver resolver = this.getContext().getContentResolver(); String str = "content://com.app.wx/student"; Uri uri = Uri.parse(str); Cursor cursor = resolver.query(uri, new String[] { "_id", "name,age,gender" }, null, null, "_id desc"); Log.d("------count",cursor.getCount()+""); } public void delete() { ContentResolver resolver = this.getContext().getContentResolver(); String str = "content://com.app.wx/student/2"; Uri uri = Uri.parse(str); long id=resolver.delete(uri, null, null); } }
執行insert方法以後(執行了三次):
執行了update方法以後:
執行了query方法以後:
執行了delete方法以後: