ContentProvider介绍

来源:互联网 发布:无线传输网络 编辑:程序博客网 时间:2024/05/05 00:12

1. 什么是ContentProvider

android系统中数据都是私有的,通过ContentProvider进行程序间数据的交换.一个Content Provider类实现了一组标准的方法接口,从而能够让其他的应用保存货读取此Content Provider的各种数据类型.

也就是说,一个程序可以通过实现一个Content Provider的抽象接口将自己的数据暴露出去,外界可以通过Content Provider的标准及统一的接口和程序里的数据打交道,可以读取程序的数据,也可以删除程序的数据.当然,中间也涉及一些权限的问题.

query(Uri uri , String[] projection , String selection , String[] selectionArgs , String sortOrder); 通过Uri进行查询,返回一个Cursor.

insert(Uri url , ContentValues values); 将一组数据插入到Uri指定的地方

update(Uri uri , ContentValues values , String where , String[] selectionArgs ); 更新uri指定位置的数据.

delete(Uri url , String where , String[] selectionArgs); 删除指定Uri并且符合一定条件的数据.


2.什么是ContentResolver

外界的程序通过ContentResolver接口可以访问ContentProvider提供的数据 , 在Activity当中通过getContentResolver()可以得到当前应用的ContentResolver实例.ContentResolver提供的接口和ContentProvider中需要实现的接口对应.主要有以下几个:

query(Uri uri , String[] projection , String selection , String[] selectionArgs , String sortOrder);通过Uri进行查询,返回一个Cursor.

insert(Uri url , ContentValues values); 将一组数据插入到Uri指定的地方.

update(Uri url , ContentValues values , String where , String[] selectionArgs);  更新Uri指定位置的数据

delete(Uri url , String where , String[] selectionArgs); 删除指定Uri 符合一定条件的数据.


3.ContentProvider和ContentResolver中用到的Uri

在ContentProvider和ContentResolver当中用到了Uri的形式通常有两种,一种是指定全部数据,另一种是指定的某个ID的数据

content://contacts/people/ 这个Uri指定的就是全部的联系人数据

content://contacts/people/1 这个Uri指定的就是ID为1的联系人数据

在上边两个类中用到的Uri一般由三个部分组成:

第一部分是: "Content://"

第二部分是要获取的数据的一个字符串

第三部分就是ID (如果没有ID,就表示返回全部)

由于URI通常比较长,而且容易弄错,且难以理解.所以在Android中定义了一些辅助类,并且定义了一些常量来代替这些长字符串的使用.

例如: Contacts.People.CONTENT_URI  (联系人的URI)


4.使用ContentProvider 读取系统数据

protected void onCreate(Bundle savedInstanceState){    super.onCreate(savedInstanceState);    Cursor C = getContentResolver().query(Phones.CONTENT_URI,null,null,null,null);    startManagingCursor(C);    ListAdapter adapter = new SimpleCursorAdapter(this, Android.R.layout.simple_list_item_2, C , new String[] {Phones.NAME , Phones.NUMBER}, new int[] {Sndroid.R.id.text1 , Android.R.id.text2});    setListAdapter(adapter);}
代码解释:

getContentResolver()方法得到应用的ContentResolver实例

query(Phones.CONTENT_URI , null , null , null , null) 它是ContentResolver里的方法,负责查询所有联系人,并返回一个Cursor.这个方法里的具体含义是:

第一个参数为Uri ,  这里是联系人的Uri

第二个参数是一个字符串的数组,数组里边的每一个字符串都是数据表中某一列的名字,它指定返回数据表中那些列的值.

第三个参数相当于SQL语句的where部分,描述哪些值是我们需要的.

第四个参数是一个字符串数组,它里边的值依次代替在第三个参数中出现的"?"符号

第五个参数指定了排列的方式.

startManagingCursor(C)语句让系统来管理生成的Cursor.

ListAdapter adapter = new SimpleCursorAdapter(this , Android.R.layout.simple_list_item_2 , C , new String[]{ Phones.NAME , Phones.NUMBER} , new int[]{Android.R.id.text1 , Android.R.id.text2})语句生成一个SimpleCursorAdapter.

====================

public SimpleAdapter(Context context , List<? extends Map<String,?>> data , int resource , String[] from , int[] to)

函数解释: Context是上下文,

Data是基于Map的List. Data里边的每一项都和ListView里边的每一项对应.Data里边的每一项是一个Map类型 ,这个Map类里边包含了ListView每一行需要的数据.比较常用的用法是: data = new ArrayList<Map<String,Object>>();

Resource,就是一个layout,可以自己定义,也可以用系统的,Android.R.layout.simple_list_item_single_choice  , Android.R.layout.two_line_list_item等

From,是一个名字的数组,每一个名字是为了在ArrayList中的每一个item中索引Map<String,Object>的Object用的.

To是一个TextView的数组.这些TextView是以id的形式来表示的.

=====================

setListAdapter(adapter)  将ListView和SimpleCursorAdapter进行绑定.



0 0
原创粉丝点击