Android content provider

来源:互联网 发布:淘宝女装旗袍 编辑:程序博客网 时间:2024/06/01 08:41


检索数据

1content provider basics

2、检索content provider

3、修改content provider数据

4、创建自己的content provider

 

1content provider basics

所有的content provider都会实现一个公共的接口

,可以实现对content provider的增删改查

activity中需要通过接口函数getContentResolver()方法获取

ContentResolver 对象,

 

content provider数据模型

 

content provider的访问

通过content providerUri可以访问到数据集,掌管多个数据集(多个数据表)的content provider为每个数据集都提供不同的Uri,所有的Uri都已content://开头,表明该数据由一个content provider掌管。如果定义了一个contentprovider,那么一般要为该content providerUri定义一个常量,例如在android平台中为存储联系人电话号码和联系人图片的Uri常量CONTENT_URI,定义的常量值分别为

android.provider.Contacts.Phones.CONTENT_URI 
android.provider.Contacts.Photos.CONTENT_URI

这两个Uri都被 Contacts content provider掌管

Uri常量也是 ContentResolver函数的第一个参数,指明需要访问的目标provider和目标content provider的哪个表,

 

2、检索content provider

检索provider需要的信息

              |---Uri(数据表)

              |---字段名

              |---字段的数据类型

如果指定查询行,需要改行的id

查询content provider可以使用下面两个函数

 ContentResolver.query()

 Activity.managedQuery()

这两个函数参数相同,而且都返回Cursor对象

managedQuery()方法会使activity管理Cursor的生命周期

 

两个函数的参数

第一个参数:provider URI 

如果指定某一条记录,可以将该条记录的_id添加到Uri的后面,例如某条记录的id123,那么该Uri

content://. . . ./123

 

有两个助手函数

 ContentUris.withAppendedId()  Uri.withAppendedPath()

可以实现在Uri后面添加id

 

Both arestatic methods that return a Uri object with the ID added

// Use the ContentUris method to produce the base URI for the contact with _ID == 23.Uri myPerson = ContentUris.withAppendedId(People.CONTENT_URI, 23);// Alternatively, use the Uri method to produce the base URI.// It takes a string rather than an integer.Uri myPerson = Uri.withAppendedPath(People.CONTENT_URI, "23");// Then query for this specific record:Cursor cur = managedQuery(myPerson, null, null, null, null);

第二个参数:

字段名:null代表所有字段,否则只会返回字段名指定的字段,android系统为其自己的

Content provider的字段名都提供了常量

第三个参数:

过滤条件:null代表返回所有

第四个参数: Selection arguments

第五个参数:指定返回数据的排序方式,null代表按照默认顺序返回,可能是无序的

一个完整例子

import android.provider.Contacts.People;import android.database.Cursor;// Form an array specifying which columns to return. String[] projection = new String[] {                             People._ID,                             People._COUNT,                             People.NAME,                             People.NUMBER                          };// Get the base URI for the People table in the Contacts content provider.Uri contacts =  People.CONTENT_URI;// Make the query. Cursor managedCursor = managedQuery(contacts,                         projection, // Which columns to return                          null,       // Which rows to return (all rows)                         null,       // Selection arguments (none)                         // Put the results in ascending order by name                         People.NAME + " ASC");

返回的结果

读取返回的数据

返回的Cursor对象可能没有数据或者一行或者多行

如果想得到返回结果某行某个字段的值,必须知道该字段的数据类型,

However, for most types, if you call the methodfor reading strings, the Cursor object will give you the String representationof the data

因为返回的Cursor对象对不同的数据类型需要用不同的方法getString()getInt(), and getFloat().读取,方法的参数为整型的字段索引,可以通过getColumnIndex函数根据字段名获得在返回结果中的索引,取得结果示例

import android.provider.Contacts.People;private void getColumnData(Cursor cur){     if (cur.moveToFirst()) {        String name;         String phoneNumber;         int nameColumn = cur.getColumnIndex(People.NAME);         int phoneColumn = cur.getColumnIndex(People.NUMBER);        String imagePath;             do {            // Get the field values            name = cur.getString(nameColumn);            phoneNumber = cur.getString(phoneColumn);                       // Do something with the values.             ...         } while (cur.moveToNext());    }}

ModifyingData

Data kept by a content provider can be modified by:

Adding new records

Adding new values to existing records

Batch updating existing records

Deleting records

 

数据修改是通过ContentResolver中的方法,有时候某个 content providers在写入数据比读取数据需要更严格的权限控制 ,如果没有对content provider的操作权限,函数执行失败

 

 content providers 

主要分为两步:

       |---创建一个map集合对象 ContentValues,并将给该集合赋值,

    |--调用ContentResolver.insert() ,将content providerUriContentValues集合对象传入,该函数返回插入记录的Uri,即包含了该插入行的idUri

import android.provider.Contacts.People;import android.content.ContentResolver;import android.content.ContentValues; ContentValues values = new ContentValues();// Add Abraham Lincoln to contacts and make him a favorite.values.put(People.NAME, "Abraham Lincoln");// 1 = the new contact is added to favorites// 0 = the new contact is not added to favoritesvalues.put(People.STARRED, 1);Uri uri = getContentResolver().insert(People.CONTENT_URI, values);

 

 

Addingnew values to existing records

对于存在的一条记录,可以给该记录添加新的字段值或者之直接修改字段值,例如为人员记录添加email地址,修改phoneNumber

 

 

原创粉丝点击