“查”功能模块:
1.核心函数:
返回类型Cursor,Cursor管理查询返回的一张表
各参数:Uri具体到表名,projection是要查寻的字段,selection和selectionArgs一起决定查询条件,sortOrder决定如何排序
(详细内容见本文最后的资源链接)
2.完成该功能核心组成:
2.1.确定并引入数据库,eg(android内置的手机联系人数据库):
import android.provider.Contacts.People;2.2.在manifest.xml中声明权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.amaker.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="4" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".ContentProviderActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
</manifest>
2.3.根据需要获取相应信息,eg(获取所有联系人的姓名,注意因为本例中要在ListView中显示结果,所以获取_ID字段)
import android.database.Cursor;
...
projection=new String[] {People._ID,People.NAME};
selection=null;
selectionArgs=null;
sortOrder=null;
c=getContentResolver().query(People.CONTENT_URI, projection, selection, selectionArgs, sortOrder);
到此时获取的查询结果都存放在Cursor 的实例c中,c提供了对结果数据管理的各种函数(详细内容见本文最后的资源链接)
2.4.对结果的后续处理,eg(利用ListView显示出来)
import android.app.ListActivity;
import android.widget.ListAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
...
adapter=new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, c,new String[] {People.NAME}, new int[] {android.R.id.text1},0);
setListAdapter(adapter);
(SimpleCursorAdapter函数详解见本文最后的资源链接)
“增”功能模块:
1.核心函数:
ContentValues's instance(values) is used to store a set of values that the ContentResolver
can process.
(详解见本文最后的资源链接)
2.完成该功能模块的核心组成:
2.1.确定并引入要写入的数据库,eg(同上一功能模块)
2.2.在manifest.xml中声明对相应数据库写的权限,eg(声明对Contacts的写入权限)
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
2.3.用ContentValues实例来承载我们要插入的记录,eg(载入姓名为"Zuhang"的联系人)
import android.content.ContentValues;
...
private ContentValues peopleToInsert;
...
peopleToInsert=new ContentValues();
peopleToInsert.put(People.NAME, "Zuhang");
2.4.调用insert()函数将记录写入到数据库(如果需要,可以用一个Uri实例来接收insert()函数的返回值),eg
getContentResolver().insert(People.CONTENT_URI, peopleToInsert);
2.5.根据需要添加后续操作,eg(将插入后的所有联系人列出来)
参考上一个功能模块
“改”功能模块:
1.核心函数:
该类属于ContentResolver
参数:Uri指名要更该的地方(注意和其他功能不同,update的参数Uri实例要具体到某一行,可以使用Uri.withAppendedId(...)或Uri.withAppendedPath(..)来实现);ContentValues承载要更改内容,形式与“增”功能模块类似;后两个单数指定查询条件
2.完成该功能的核心组成:
2.1.确定并引入要写入的数据库,eg(同上一功能模块)
2.2.在manifest.xml中声明对相应数据库写的权限,eg(同上一功能模块)
2.3.用ContentValues实例来承载要更该的内容,形式类似于插入模块,eg(本例中将名字为“Zuhang”的第一条记录名字改为“nihao”)
import android.content.ContentValues;
...
private ContentValues peopleToUpdate;
...
peopleToInsert=new ContentValues();
peopleToInsert.put(People.NAME, "nihao");
2.4.利用“查”功能和Uri获取具体记录的Uri实例,eg(本例获取名字为“Zuhang”的第一条记录的Uri)
String[] projectionUpdate=new String[] {People._ID,People.NAME};
Cursor cUpdate=getContentResolver().query(People.CONTENT_URI, projectionUpdate, null, null, null);
cUpdate.moveToFirst();
Uri uri=Uri.withAppendedPath(People.CONTENT_URI, cUpdate.getString(cUpdate.getColumnIndex(People._ID)));
2.5.调用update函数跟新数据,eg
peopleToUpdate=new ContentValues();
peopleToUpdate.put(People.NAME,"nihao");
selectionUpdate=new String(People.NAME+"='Zuhang'");
selectionUpdateArgs=null;
getContentResolver().update(uri, peopleToUpdate, selectionUpdate, null);
2.6.其他后续操作
“删”功能模块:
1.核心函数:
Uri只需具体到表,其他实现与前类似
本文涉及的外部资源:
1.query/insert/update/delete函数详解
2.Cursor类官方文档
3.类SimpleCursorAdapter官方文档
4.ContentValues类官方文档
5.Uri官方文档
6.本文对应的实例源代码