内容提供者ContentProvider →采用layoutInflater打气筒创建一个view对象
2014-07-31 12:56
477 查看
org.gentry.db.PersonDBProvider.java
新建一个工程other:对数据库中的信息进行操作(增删改查)
xml
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(); } }
相关文章推荐
- 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)
- [android] 采用layoutInflater打气筒创建一个view对象
- 采用layoutInflater打气筒创建一个view对象 →ListView
- listview入门(采用layoutlnflater打气筒创建一个view对象)
- 用 layoutInflater打气筒创建一个view对象
- Android 采用Layout Inflater创建一个View对象
- Android 漫游之路------ListView入门、Layout的Inflater创建View对象、常用数据适配器
- Android -- listview的使用, Inflater打气筒创建View对象,三种数据Adapter
- LayoutInflater拦截View创建,自定义Resource对象
- Android -- listview的使用, Inflater打气筒创建View对象,三种数据Adapter
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- inflate用一个XML源填充view. LayoutInflater
- 如何创建一个UIImageView对象去显示图片
- Android -- ContentProvider 内容提供者,创建和调用
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- 6-2-1 字符串-字符串变量String-创建字符串变量-初始化字符串变量-字符串连接-输入字符串(单词、行)-对象变量的赋值-比较两个字符串是否同一个-比较两个字符串内容是否相同
- Android学习---通过内容提供者(ContentProvider)操作另外一个应用私有数据库的内容
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- Android LayoutInflater 【 一个类似于findViewById的神器】
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byt