ContentProvider是用來實現不同應用程序之間數據的相互訪問,不僅僅是可以訪問數據庫,像xml文件等都可以訪問
爲contentProvider設置觀察者,
首先自定義觀察者:
class MyObserver extends ContentObserver
{
public MyObserver(Handler handler) {
super(handler);
// TODO Auto-generated constructor stub
}
@Override
public void onChange(boolean selfChange) {
c=dao.queryCursor();
//每次改變時都會觸發次方法,這時對數據庫進行重新查詢,並重新爲listView添加adapter
listView.setAdapter(new SimpleCursorAdapter(getApplicationContext(), R.layout.item, c, new String[] { "_id", "name", "balance" },
new int[] { R.id.personid, R.id.name, R.id.balance }));
}
}
然後進行註冊:註冊一般是在activity中進行
Uri uri=Uri.parse("content://com.example.sqlite");//這樣設置uri可以觀察com.example.sqlite下所有表的數據庫的改變
getContentResolver().registerContentObserver(uri, true, new MyObserver(new Handler()));
要實現contentObserver對增刪改查的監聽,並能及時的更新改變的信息,再每次增刪改差時要通知觀察者
getContext().getContentResolver().notifyChange(uri, null);
通過繼承contentProvider實現增刪改查:
package com.example.sqllite;
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 MyContentProvider extends ContentProvider {
UriMatcher matcher;
public static final int PERSON = 1;
public static final int NUMBER = 2;
MyDBOpenHelper db;
// 但應用部署到手機上時,觸發此方法
@Override
public boolean onCreate() {
System.out.println("創建了");
db = new MyDBOpenHelper(getContext());
matcher = new UriMatcher(UriMatcher.NO_MATCH);
matcher.addURI("com.example.sqlite", "person", PERSON);//查詢的是person表
matcher.addURI("com.example.sqlite", "person/#", NUMBER);//查詢的是person表中某個id
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteDatabase database = db.getReadableDatabase();
int code = matcher.match(uri);
System.out.println("code" + code);
switch (code) {
case PERSON:
return database.query("person", projection, selection,
selectionArgs, null, null, null);
case NUMBER:
int id = (int) ContentUris.parseId(uri);//提取uri中的id
selection = (selection == null) ? "id=" + id : selection//判斷selection是否爲空,如果不爲空再在原來條件的基礎上添加id的條件
+ "and id=" + id;
return database.query("person", projection, selection,
selectionArgs, null, null, null);
default:
throw new RuntimeException("uri無法陪陪" + uri);
}
}
// 返回的是mimeType類型
@Override
public String getType(Uri uri) {
switch (matcher.match(uri)) {
case PERSON:
return "vnd.android.cursor.dir/person"; //返回的是mimetype
case NUMBER:
return "vnd.android.cursor.item/person";
default:
break;
}
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
int code = matcher.match(uri);
switch (code) {
case PERSON:
SQLiteDatabase data = db.getWritableDatabase();
data.insert("person", "name", values);// 第二個參數是name,是因爲防止values爲空,報錯
getContext().getContentResolver().notifyChange(uri, null);
break;
default:
throw new RuntimeException("uri無法陪陪" + uri);
}
return null;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int code = matcher.match(uri);
SQLiteDatabase data=db.getWritableDatabase();
switch (code) {
case PERSON:
data.delete("person",selection,selectionArgs);
getContext().getContentResolver().notifyChange(uri, null);
break;
case NUMBER:
int id=(int) ContentUris.parseId(uri);
selection=(selection==null)?"id="+id:selection+"and id="+id;
data.delete("person", selection, selectionArgs);
getContext().getContentResolver().notifyChange(uri, null);
break;
default:
break;
}
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
SQLiteDatabase data=db.getWritableDatabase();
int code=matcher.match(uri);
switch (code) {
case PERSON:
data.update("person", values, selection, selectionArgs);
getContext().getContentResolver().notifyChange(uri, null);
break;
case NUMBER:
int id=(int) ContentUris.parseId(uri);
selection=(selection==null)?"id="+id:selection+"and id="+id;
data.update("person", values, selection, selectionArgs);
getContext().getContentResolver().notifyChange(uri, null);
break;
default:
break;
}
return 0;
}
}