内容提供者ContentProvider →采用layoutInflater打气筒创建一个view对象
来源:互联网 发布:合并矩阵 编辑:程序博客网 时间:2024/05/01 09:40
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,如果路径不满足条件,返回-1private 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 查询的操作@Overridepublic int delete(Uri arg0, String arg1, String[] arg2) {// TODO Auto-generated method stubif (matcher.match(arg0) == DELETE) {// 返回删除的结果集SQLiteDatabase db = helper.getWritableDatabase();db.delete("person", arg1, arg2);} else {throw new IllegalArgumentException("路径不匹配,不能执行删除操作");}return 0;}@Overridepublic String getType(Uri uri) {// TODO Auto-generated method stubif (matcher.match(uri) == QUERY) {// 返回多条数据return "vnd.android.cursor.dir/person";} else if (matcher.match(uri) == QUERYONE) {// 返回一条数据return "vnd.android.cursor.item/person";}return null;}@Overridepublic Uri insert(Uri uri, ContentValues values) {// TODO Auto-generated method stubif (matcher.match(uri) == INSERT) {// 返回插入的结果集SQLiteDatabase db = helper.getWritableDatabase();db.insert("person", null, values);} else {throw new IllegalArgumentException("路径不匹配,不能执行插入操作");}return null;}/** * 当内容提供者被创建的时候调用,适合数据的初始化 */@Overridepublic boolean onCreate() {// TODO Auto-generated method stubhelper = new PersonSQLiteOpenHelper(getContext());return false;}@Overridepublic Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) {// TODO Auto-generated method stubif (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("路径不匹配,不能执行查询操作");}}@Overridepublic int update(Uri uri, ContentValues values, String selection,String[] selectionArgs) {// TODO Auto-generated method stubif (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 {@Overrideprotected 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();}}
0 0
- 内容提供者ContentProvider →采用layoutInflater打气筒创建一个view对象
- 采用layoutInflater打气筒创建一个view对象 →ListView
- 用 layoutInflater打气筒创建一个view对象
- listview入门(采用layoutlnflater打气筒创建一个view对象)
- Android -- ContentProvider 内容提供者,创建和调用
- ContentProvider内容提供者
- Android ContentProvider(内容提供者)
- ContentProvider 内容提供者
- ContentProvider内容提供者(一)
- ContentProvider内容提供者(二)
- ContentProvider 内容提供者
- android内容提供者ContentProvider
- Android--- ContentProvider(内容提供者)
- ContentProvider内容提供者
- ContentProvider(内容提供者)
- ContentProvider(内容提供者)
- ContentProvider内容提供者
- ContentProvider 内容提供者
- 总结安卓开发中常用的七个第三方框架
- day122(7.13)性能测试知识总结V
- Enterprise Architect使用问题记录
- JavaScript基础-window.location的用法
- objective-c 字符串筛选数字(不使用正则表达式)
- 内容提供者ContentProvider →采用layoutInflater打气筒创建一个view对象
- 第一个ios应用(2)
- linux mysql 备份数据库
- CentOS、Ubuntu、Debian三个linux比较异同
- Hadoop 新 MapReduce 框架 Yarn 详解
- java异常
- jvm如何判断对象已死?
- 分隔字符串,存取到vector中
- 递归函数 const关键字