
MainActivity如下:
复制代码 代码如下:
package cn.testbaIDu;
import androID.net.Uri;
import androID.os.Bundle;
import androID.vIEw.VIEw;
import androID.vIEw.VIEw.OnClickListener;
import androID.Widget.button;
import androID.app.Activity;
import androID.content.ContentResolver;
import androID.content.ContentValues;
import androID.database.Cursor;
/**
* Demo描述:
* 应用A(TestBaIDu)调用另外一个应用(TestContentProvIDer)
* 中的自定义ContentProvIDer,即:
* 1 自定义ContentProvIDer的使用
* 2 其它应用调用该ContentProvIDer
*
* 测试方法:
* 1 依次测试ContentProvIDer的增查删改(注意该顺序)
* 2 其它应用查询该ContentProvIDer的数据
*
*/
public class MainActivity extends Activity {
private button mAddbutton;
private button mDeletebutton;
private button mUpdatebutton;
private button mquerybutton;
private button mTypebutton;
private ContentResolver mContentResolver;
@OverrIDe
protected voID onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentVIEw(R.layout.main);
init();
}
private voID init() {
mContentResolver=this.getContentResolver();
mAddbutton=(button) findVIEwByID(R.ID.addbutton);
mAddbutton.setonClickListener(new ClickListenerImpl());
mDeletebutton=(button) findVIEwByID(R.ID.deletebutton);
mDeletebutton.setonClickListener(new ClickListenerImpl());
mUpdatebutton=(button) findVIEwByID(R.ID.updatebutton);
mUpdatebutton.setonClickListener(new ClickListenerImpl());
mquerybutton=(button) findVIEwByID(R.ID.querybutton);
mquerybutton.setonClickListener(new ClickListenerImpl());
mTypebutton=(button) findVIEwByID(R.ID.typebutton);
mTypebutton.setonClickListener(new ClickListenerImpl());
}
private class ClickListenerImpl implements OnClickListener{
@OverrIDe
public voID onClick(VIEw v) {
switch (v.getID()) {
case R.ID.addbutton:
Person person=null;
for (int i = 0; i < 5; i++) {
person=new Person("xiaoming"+i,"9527"+i,(8888+i));
testInsert(person);
}
break;
case R.ID.deletebutton:
testDelete(1);
break;
case R.ID.updatebutton:
testUpdate(3);
break;
case R.ID.querybutton:
//查询表
//queryFromContentProvIDer(-1);
//查询personID=2的数据
testquery(2);
break;
case R.ID.typebutton:
testType();
break;
default:
break;
}
}
}
private voID testInsert(Person person) {
ContentValues contentValues=new ContentValues();
contentValues.put("name",person.getname());
contentValues.put("phone",person.getPhone());
contentValues.put("salary",person.getSalary());
Uri insertUri=Uri.parse("content://cn.bs.testcontentprovIDer/person");
Uri returnUri=mContentResolver.insert(insertUri,contentValues);
System.out.println("新增数据:returnUri="+returnUri);
}
private voID testDelete(int index){
Uri uri=Uri.parse("content://cn.bs.testcontentprovIDer/person/"+String.valueOf(index));
mContentResolver.delete(uri,null,null);
}
private voID testUpdate(int index){
Uri uri=Uri.parse("content://cn.bs.testcontentprovIDer/person/"+String.valueOf(index));
ContentValues values=new ContentValues();
values.put("name","hanmeimei");
values.put("phone","1234");
values.put("salary",333);
mContentResolver.update(uri,values,null);
}
private voID testquery(int index) {
Uri uri=null;
if (index<=0) {
//查询表
uri=Uri.parse("content://cn.bs.testcontentprovIDer/person");
} else {
//按照ID查询某条数据
uri=Uri.parse("content://cn.bs.testcontentprovIDer/person/"+String.valueOf(index));
}
//对应上面的:查询表
//Cursor cursor= mContentResolver.query(uri,null);
//对应上面的:查询personID=2的数据
//注意:因为name是varchar字段的,所以应该写作"name='xiaoming1'"
// 若写成"name=xiaoming1"查询时会报错
Cursor cursor= mContentResolver.query(uri,"name='xiaoming1'",null);
while(cursor.movetoNext()){
int personID=cursor.getInt(cursor.getColumnIndex("personID"));
String name=cursor.getString(cursor.getColumnIndex("name"));
String phone=cursor.getString(cursor.getColumnIndex("phone"));
int salary=cursor.getInt(cursor.getColumnIndex("salary"));
System.out.println("查询得到:personID=" + personID+",name="+name+",phone="+phone+",salary="+salary);
}
cursor.close();
}
private voID testType(){
Uri dirUri=Uri.parse("content://cn.bs.testcontentprovIDer/person");
String dirType=mContentResolver.getType(dirUri);
System.out.println("dirType:"+dirType);
Uri itemUri=Uri.parse("content://cn.bs.testcontentprovIDer/person/3");
String itemType=mContentResolver.getType(itemUri);
System.out.println("itemType:"+itemType);
}
}
Person如下:
复制代码 代码如下:
package cn.testbaIDu;
public class Person {
private Integer ID;
private String name;
private String phone;
private Integer salary;
public Person(String name,String phone,Integer salary) {
this.name = name;
this.phone = phone;
this.salary=salary;
}
public Person(Integer ID,String name,Integer salary) {
this.ID = ID;
this.name = name;
this.phone = phone;
this.salary=salary;
}
public Integer getID() {
return ID;
}
public voID setID(Integer ID) {
this.ID = ID;
}
public String getname() {
return name;
}
public voID setname(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public voID setPhone(String phone) {
this.phone = phone;
}
public Integer getSalary() {
return salary;
}
public voID setSalary(Integer salary) {
this.salary = salary;
}
@OverrIDe
public String toString() {
return "Person [ID=" + ID + ",name=" + name + ",phone=" + phone+ ",salary=" + salary + "]";
}
}
main.xml如下:
复制代码 代码如下:
<relativeLayout 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" >
<button
androID:ID="@+ID/addbutton"
androID:layout_wIDth="wrap_content"
androID:layout_height="wrap_content"
androID:layout_centerHorizontal="true"
androID:layout_margintop="30dip"
androID:text="增加"
androID:textSize="20sp" />
<button
androID:ID="@+ID/deletebutton"
androID:layout_wIDth="wrap_content"
androID:layout_height="wrap_content"
androID:layout_centerHorizontal="true"
androID:layout_margintop="30dip"
androID:layout_below="@ID/addbutton"
androID:text="删除"
androID:textSize="20sp" />
<button
androID:ID="@+ID/updatebutton"
androID:layout_wIDth="wrap_content"
androID:layout_height="wrap_content"
androID:layout_centerHorizontal="true"
androID:layout_margintop="30dip"
androID:layout_below="@ID/deletebutton"
androID:text="修改"
androID:textSize="20sp" />
<button
androID:ID="@+ID/querybutton"
androID:layout_wIDth="wrap_content"
androID:layout_height="wrap_content"
androID:layout_centerHorizontal="true"
androID:layout_margintop="30dip"
androID:layout_below="@ID/updatebutton"
androID:text="查询"
androID:textSize="20sp" />
<button
androID:ID="@+ID/typebutton"
androID:layout_wIDth="wrap_content"
androID:layout_height="wrap_content"
androID:layout_centerHorizontal="true"
androID:layout_margintop="30dip"
androID:layout_below="@ID/querybutton"
androID:text="类型"
androID:textSize="20sp" />
</relativeLayout>
//以下为TestContentProvIDer
MainActivity如下:
复制代码 代码如下:
package cn.testcontentprovIDer;
import androID.app.Activity;
import androID.os.Bundle;
public class MainActivity extends Activity {
@OverrIDe
protected voID onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentVIEw(R.layout.main);
}
}
ContentProvIDerTest如下:
复制代码 代码如下:
package cn.testcontentprovIDer;
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;
/**
* 注意事项:
* 在AndroIDManifest.xml中注册ContentProvIDer时的属性
* androID:exported="true"表示允许其他应用访问.
* 这样TestBaIDu这个应用才可以访问该处的ContentProvIDer
*/
public class ContentProvIDerTest extends ContentProvIDer {
private DBOpenHelper dbOpenHelper;
private UriMatcher URI_MATCHER;
private static final int PERSONS = 0;
private static final int PERSON = 1;
@OverrIDe
public boolean onCreate() {
initUriMatcher();
dbOpenHelper=new DBOpenHelper(getContext());
return true;
}
//初始化UriMatcher
private voID initUriMatcher(){
URI_MATCHER=new UriMatcher(UriMatcher.NO_MATCH);
//表示返回所有的person,其中PERSONS为该特定Uri的标识码
URI_MATCHER.addURI("cn.bs.testcontentprovIDer","person",PERSONS);
//表示返回某一个person,其中PERSON为该特定Uri的标识码
URI_MATCHER.addURI("cn.bs.testcontentprovIDer","person/#",PERSON);
}
/**
* 插入 *** 作:
* 插入 *** 作只有一种可能:向一张表中插入
* 返回结果为新增记录对应的Uri
* 方法db.insert()返回结果为新增记录对应的主键值
*/
@OverrIDe
public Uri insert(Uri uri,ContentValues values) {
sqliteDatabase db=dbOpenHelper.getWritableDatabase();
switch (URI_MATCHER.match(uri)) {
case PERSONS:
long rowID=db.insert("person","name,phone,salary",values);
return ContentUris.withAppendedID(uri,rowID);
default:
throw new IllegalArgumentException("unkNown uri"+uri.toString());
}
}
/**
* 更新 *** 作:
* 更新 *** 作有两种可能:更新一张表或者更新某条数据
* 在更新某条数据时原理类似于查询某条数据,见下.
*/
@OverrIDe
public int update(Uri uri,ContentValues values,String selection,String[] selectionArgs) {
sqliteDatabase db=dbOpenHelper.getWritableDatabase();
int updatanum=0;
switch (URI_MATCHER.match(uri)) {
//更新表
case PERSONS:
updatanum=db.update("person",selection,selectionArgs);
break;
//按照ID更新某条数据
case PERSON:
long ID=ContentUris.parseID(uri);
String where="personID="+ID;
if(selection!=null&&!"".equals(selection.trim())){
where=selection+" and "+where;
}
updatanum=db.update("person",where,selectionArgs);
break;
default:
throw new IllegalArgumentException("unkNown uri"+uri.toString());
}
return updatanum;
}
/**
* 删除 *** 作:
* 删除 *** 作有两种可能:删除一张表或者删除某条数据
* 在删除某条数据时原理类似于查询某条数据,见下.
*/
@OverrIDe
public int delete(Uri uri,String[] selectionArgs) {
sqliteDatabase db=dbOpenHelper.getWritableDatabase();
int deletednum=0;
switch (URI_MATCHER.match(uri)) {
//删除表
case PERSONS:
deletednum=db.delete("person",selectionArgs);
break;
//按照ID删除某条数据
case PERSON:
long ID=ContentUris.parseID(uri);
String where="personID="+ID;
if(selection!=null&&!"".equals(selection.trim())){
where=selection+" and "+where;
}
deletednum=db.delete("person",selectionArgs);
break;
default:
throw new IllegalArgumentException("unkNown uri"+uri.toString());
}
return deletednum;
}
/**
* 查询 *** 作:
* 查询 *** 作有两种可能:查询一张表或者查询某条数据
* 注意事项:
* 在查询某条数据时要注意--因为此处是按照personID来查询
* 某条数据,但是同时可能还有其他限制.例如:
* 要求personID为2且name为xiaoming1
* 所以在查询时分为两步:
* 第一步:
* 解析出personID放入where查询条件
* 第二部:
* 判断是否有其他限制(如name),若有则将其
* 组拼到where查询条件.
* 详细代码见下.
*/
@OverrIDe
public Cursor query(Uri uri,String[] projection,String[] selectionArgs,String sortOrder) {
sqliteDatabase db=dbOpenHelper.getWritableDatabase();
Cursor cursor;
switch (URI_MATCHER.match(uri)) {
//查询表
case PERSONS:
cursor=db.query("person",projection,selectionArgs,sortOrder);
break;
//按照ID查询某条数据
case PERSON:
//第一步:
long ID=ContentUris.parseID(uri);
String where="personID="+ID;
//第二步:
if(selection!=null&&!"".equals(selection.trim())){
where=selection+" and "+where;
}
cursor=db.query("person",sortOrder);
break;
default:
throw new IllegalArgumentException("unkNown uri"+uri.toString());
}
return cursor;
}
/**
* 返回当前Uri所代表的数据的MIME类型.
* 如果该Uri对应的数据可能包含多条记录,那么返回
* 字符串应该以"vnd.androID.cursor.dir/"开头
* 如果该Uri对应的数据只包含一条记录,那么返回
* 字符串应该以"vnd.androID.cursor.item/"开头
*/
@OverrIDe
public String getType(Uri uri) {
switch (URI_MATCHER.match(uri)) {
case PERSONS:
return "vnd.androID.cursor.dir/persons";
case PERSON:
return "vnd.androID.cursor.item/person";
default:
throw new IllegalArgumentException("unkNown uri"+uri.toString());
}
}
}
DBOpenHelper如下:
复制代码 代码如下:
package cn.testcontentprovIDer;
import androID.content.Context;
import androID.database.sqlite.sqliteDatabase;
import androID.database.sqlite.sqliteOpenHelper;
public class DBOpenHelper extends sqliteOpenHelper {
public DBOpenHelper(Context context) {
super(context,"contentprovIDertest.db",1);
}
@OverrIDe
public voID onCreate(sqliteDatabase db) {
db.execsql("create table person(personID integer primary key autoincrement,name varchar(20),phone varchar(12),salary Integer(12))");
}
//当数据库版本号发生变化时调用该方法
@OverrIDe
public voID onUpgrade(sqliteDatabase db,int arg1,int arg2) {
//db.execsql("ALTER table person ADD phone varchar(12) NulL");
//db.execsql("ALTER table person ADD salary Integer NulL");
}
}
AndroIDManifest.xml如下:
复制代码 代码如下:
<?xml version="1.0" enCoding="utf-8"?>
<manifest xmlns:androID="http://schemas.androID.com/apk/res/androID"
package="cn.testcontentprovIDer"
androID:versionCode="1"
androID:versionname="1.0" >
<uses-sdk
androID:minSdkVersion="8"
androID:targetSdkVersion="8" />
<uses-permission androID:name="androID.permission.INTERNET" />
<uses-permission androID:name="androID.permission.ACCESS_NETWORK_STATE" />
<uses-permission androID:name="androID.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission androID:name="androID.permission.MOUNT_UNMOUNT_fileSYstemS" />
<application
androID:allowBackup="true"
androID:icon="@drawable/ic_launcher"
androID:label="@string/app_name"
androID:theme="@style/Apptheme" >
<activity
androID:name="cn.testcontentprovIDer.MainActivity"
androID:label="@string/app_name" >
<intent-filter>
<action androID:name="androID.intent.action.MAIN" />
<category androID:name="androID.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provIDer
androID:name="cn.testcontentprovIDer.ContentProvIDerTest"
androID:authoritIEs="cn.bs.testcontentprovIDer"
androID:exported="true"
/>
</application>
</manifest>
总结
以上是内存溢出为你收集整理的Android中自定义ContentProvider实例全部内容,希望文章能够帮你解决Android中自定义ContentProvider实例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)