访问内容提供者(和上文联系),测试
2015-11-25 19:50
274 查看
内容提供者提供了对自定义的一个SQLite数据库的表person中增删改查,对象为person(bean)
public class TestPersonContentProvider extends AndroidTestCase
{
private static final String TAG = "TestPersonContentProvider";
public void testInsert()
{
//访问地址:content://要访问的包名.类名/person/insert
ContentResolver resolver = getContext().getContentResolver(); //内容提供者的访问类
ContentValues values = new ContentValues();
values.put("name", "sz");
values.put("age", "20");
resolver.insert(Uri.parse("content://要访问的包名.类名/person/insert"), values);
}
public void testUpdate()
{
ContentResolver resolver = getContext().getContentResolver(); //内容提供者的访问类
ContentValues values = new ContentValues();
values.put("name", "1505");
String where = "id = ?";
String[] selectionArgs = new String[]{"1"};
int id = resolver.update(
Uri.parse("content://要访问的包名.类名/person/update"),
values ,
where,
selectionArgs);
Log.d(TAG, "UPDATE .... = " + id);
}
public void testQuery()
{
ContentResolver resolver = getContext().getContentResolver(); //内容提供者的访问类
//content://要访问的包名.类名/person/query
String[] projection = new String[]{"id","name","age"};
Cursor cursor = resolver.query(
Uri.parse("content://要访问的包名.类名/person/query"),
projection,
null,
null,
null);
if(null != cursor && cursor.getCount() > 0)
{
while(cursor.moveToNext())
{
Integer id = cursor.getInt(0);
String name = cursor.getString(1);
Integer age = cursor.getInt(2);
Log.d(TAG, "id=" + id + " name= " + name + " age= " + age);
}
cursor.close(); // 一定记住不要忘了关闭游标
}
}
}
注意权限:与内容提供者中想对应的权限
<uses-permission android:name="aaa.bbb.ccc"/>
<uses-permission android:name="aaa.bbb.ccc.ddd"/>
public class TestPersonContentProvider extends AndroidTestCase
{
private static final String TAG = "TestPersonContentProvider";
public void testInsert()
{
//访问地址:content://要访问的包名.类名/person/insert
ContentResolver resolver = getContext().getContentResolver(); //内容提供者的访问类
ContentValues values = new ContentValues();
values.put("name", "sz");
values.put("age", "20");
resolver.insert(Uri.parse("content://要访问的包名.类名/person/insert"), values);
}
public void testUpdate()
{
ContentResolver resolver = getContext().getContentResolver(); //内容提供者的访问类
ContentValues values = new ContentValues();
values.put("name", "1505");
String where = "id = ?";
String[] selectionArgs = new String[]{"1"};
int id = resolver.update(
Uri.parse("content://要访问的包名.类名/person/update"),
values ,
where,
selectionArgs);
Log.d(TAG, "UPDATE .... = " + id);
}
public void testQuery()
{
ContentResolver resolver = getContext().getContentResolver(); //内容提供者的访问类
//content://要访问的包名.类名/person/query
String[] projection = new String[]{"id","name","age"};
Cursor cursor = resolver.query(
Uri.parse("content://要访问的包名.类名/person/query"),
projection,
null,
null,
null);
if(null != cursor && cursor.getCount() > 0)
{
while(cursor.moveToNext())
{
Integer id = cursor.getInt(0);
String name = cursor.getString(1);
Integer age = cursor.getInt(2);
Log.d(TAG, "id=" + id + " name= " + name + " age= " + age);
}
cursor.close(); // 一定记住不要忘了关闭游标
}
}
}
注意权限:与内容提供者中想对应的权限
<uses-permission android:name="aaa.bbb.ccc"/>
<uses-permission android:name="aaa.bbb.ccc.ddd"/>
相关文章推荐
- Android中内容观察者的使用---- ContentObserver类详解
- ubuntu 配置git公钥
- MyEclipse代码自动补全设置
- 你不知道的shell用法和脚本,简单&强大
- win8.1 文件读写(四)
- win8.1 文件读写(三)
- 通过AVFoundation获取摄像头数据[Swift]
- BZOJ1257: [CQOI2007]余数之和
- next_permutation函数
- 液晶之12864代码及中文乱码问题
- Python3.x List方法集合
- Chroot相关
- 逆秩链表
- 视频压缩预测编码和变换编码概述
- JavaScript包装对象
- LeetCode 28_Implement strStr()
- VLC 实现的简单播放器
- 黑马程序员——JDK的新特性笔记整理
- Linux学习笔记之Makefile多个c文件的编写
- 干货:2016年百度技术面试官的一些思考