您的位置:首页 > 编程语言 > PHP开发

内容提供者ContentProvider →采用layoutInflater打气筒创建一个view对象

2014-07-31 12:56 477 查看
org.gentry.db.PersonDBProvider.java

package org.gentry.db;

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 PersonDBProvider extends ContentProvider {

private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); // 定义一个uri的匹配器,用于匹配uri,如果路径不满足条件,返回-1
private static final int INSERT = 1;
private static final int DELETE = 2;
private static final int UPDATE = 3;
private static final int QUERY = 4;
private static final int QUERYONE = 5;
private PersonSQLiteOpenHelper helper;

static {
// 添加一组匹配规则
matcher.addURI("org.gentry.db.personprovider", "insert", INSERT);
matcher.addURI("org.gentry.db.personprovider", "delete", DELETE);
matcher.addURI("org.gentry.db.personprovider", "update", UPDATE);
matcher.addURI("org.gentry.db.personprovider", "query", QUERY);
matcher.addURI("org.gentry.db.personprovider", "query/#", QUERYONE);
}

// content://org.gentry.db.personprovider/insert 添加的操作
// content://org.gentry.db.personprovider/delete 删除的操作
// content://org.gentry.db.personprovider/update 更新的操作
// content://org.gentry.db.personprovider/query 查询的操作
@Override
public int delete(Uri arg0, String arg1, String[] arg2) {
// TODO Auto-generated method stub
if (matcher.match(arg0) == DELETE) {
// 返回删除的结果集
SQLiteDatabase db = helper.getWritableDatabase();
db.delete("person", arg1, arg2);
} else {
throw new IllegalArgumentException("路径不匹配,不能执行删除操作");
}
return 0;
}

@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
if (matcher.match(uri) == QUERY) {
// 返回多条数据
return "vnd.android.cursor.dir/person";
} else if (matcher.match(uri) == QUERYONE) {
// 返回一条数据
return "vnd.android.cursor.item/person";
}
return null;
}

@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
if (matcher.match(uri) == INSERT) {
// 返回插入的结果集
SQLiteDatabase db = helper.getWritableDatabase();
db.insert("person", null, values);
} else {
throw new IllegalArgumentException("路径不匹配,不能执行插入操作");
}
return null;
}

/**
* 当内容提供者被创建的时候调用,适合数据的初始化
*/
@Override
public boolean onCreate() {
// TODO Auto-generated method stub
helper = new PersonSQLiteOpenHelper(getContext());
return false;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO Auto-generated method stub
if (matcher.match(uri) == QUERY) {
// 返回查询的结果集
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.query("person", projection, selection,
selectionArgs, null, null, sortOrder);
return cursor;
} else if (matcher.match(uri) == QUERYONE) {
long id = ContentUris.parseId(uri);
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.query("person", projection, "id=?",
new String[] { id + "" }, null, null, sortOrder);
return cursor;
} else {
throw new IllegalArgumentException("路径不匹配,不能执行查询操作");
}
}

@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
if (matcher.match(uri) == UPDATE) {
// 返回修改的结果集
SQLiteDatabase db = helper.getWritableDatabase();
db.update("person", values, selection, selectionArgs);
} else {
throw new IllegalArgumentException("路径不匹配,不能执行修改操作");
}
return 0;
}

}


新建一个工程other:对数据库中的信息进行操作(增删改查)

xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="insert"
android:text="添加db的数据" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="delete"
android:text="删除db的数据" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="update"
android:text="修改db的数据" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="query"
android:text="读取db的数据" />

</LinearLayout>


java

package org.gentry.other;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}

public void insert(View view) {
ContentResolver resolver = getContentResolver();
Uri uri = Uri.parse("content://org.gentry.db.personprovider/insert");
ContentValues values = new ContentValues();
values.put("number", "999");
values.put("name", "zhaoqi");
resolver.insert(uri, values);
}

public void delete(View view) {
ContentResolver resolver = getContentResolver();
Uri uri = Uri.parse("content://org.gentry.db.personprovider/delete");
resolver.delete(uri, "name=?", new String[] { "wangwu1" });
}

public void update() {
ContentResolver resolver = getContentResolver();
Uri uri = Uri.parse("content://org.gentry.db.personprovider/update");
ContentValues values = new ContentValues();
values.put("number", "110");
resolver.update(uri, values, "name=?", new String[] { "wangwu3" });
}

public void query(View view) {
// 得到手机的中间人
ContentResolver resolver = getContentResolver();
Uri uri = Uri.parse("content://org.gentry.db.personprovider/query");
Cursor cursor = resolver.query(uri, null, null, null, null);
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
String id = cursor.getString(cursor.getColumnIndex("id"));
System.out.println("name=" + name + ":" + "id=" + id);
}
cursor.close();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐