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;
}
}