内容提供者访问
来源:互联网 发布:金山恢复软件 编辑:程序博客网 时间:2024/06/08 13:56
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.test.AndroidTestCase;
import android.util.Log;
public class TextCase extends AndroidTestCase {
private static final String TAG = "TextCase";
public void testInsert() {
Uri uri = Uri.parse("content://com.itheima28.sqlitedemo.providers.PersonContentProvider/person/insert");
// 内容提供者访问对象
ContentResolver resolver = getContext().getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "fengjie");
values.put("age", 90);
uri = resolver.insert(uri, values);
Log.i(TAG, "uri: " + uri);
long id = ContentUris.parseId(uri);
Log.i(TAG, "添加到: " + id);
}
public void testDelete() {
Uri uri = Uri.parse("content://com.itheima28.sqlitedemo.providers.PersonContentProvider/person/delete");
// 内容提供者访问对象
ContentResolver resolver = getContext().getContentResolver();
String where = "_id = ?";
String[] selectionArgs = {"21"};
int count = resolver.delete(uri, where, selectionArgs);
Log.i(TAG, "删除行: " + count);
}
public void testUpdate() {
Uri uri = Uri.parse("content://com.itheima28.sqlitedemo.providers.PersonContentProvider/person/update");
// 内容提供者访问对象
ContentResolver resolver = getContext().getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "lisi");
int count = resolver.update(uri, values, "_id = ?", new String[]{"20"});
Log.i(TAG, "更新行: " + count);
}
public void testQueryAll() {
Uri uri = Uri.parse("content://com.itheima28.sqlitedemo.providers.PersonContentProvider/person/queryAll");
// 内容提供者访问对象
ContentResolver resolver = getContext().getContentResolver();
Cursor cursor = resolver.query(uri, new String[]{"_id", "name", "age"}, null, null, "_id desc");
if(cursor != null && cursor.getCount() > 0) {
int id;
String name;
int age;
while(cursor.moveToNext()) {
id = cursor.getInt(0);
name = cursor.getString(1);
age = cursor.getInt(2);
Log.i(TAG, "id: " + id + ", name: " + name + ", age: " + age);
}
cursor.close();
}
}
public void testQuerySingleItem() {
Uri uri = Uri.parse("content://com.itheima28.sqlitedemo.providers.PersonContentProvider/person/query/#");
// 在uri的末尾添加一个id content://com.itheima28.sqlitedemo.providers.PersonContentProvider/person/query/20
uri = ContentUris.withAppendedId(uri, 20);
// 内容提供者访问对象
ContentResolver resolver = getContext().getContentResolver();
Cursor cursor = resolver.query(uri, new String[]{"_id", "name", "age"}, null, null, null);
if(cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(0);
String name = cursor.getString(1);
int age = cursor.getInt(2);
cursor.close();
Log.i(TAG, "id: " + id + ", name: " + name + ", age: " + age);
}
}
}
0 0
- 内容提供者访问
- 访问内容提供者
- 访问内容提供者
- 内容提供者访问通讯录
- 访问内容提供者
- Android 访问自定义内容提供者
- Android_09_自定义内容提供者及访问内容提供者
- 内容提供者创建,访问和权限
- android_访问系统短信内容提供者
- ContentProvider内容提供者,跨进程访问方式
- 内容提供者
- 内容提供者
- 内容提供者
- 内容提供者
- 内容提供者
- 内容提供者
- 内容提供者
- 内容提供者
- EOF是什么?
- 【BZOJ1832】【AHOI2008】聚会 倍增lca
- 小鑫爱运动
- fan qiang
- 文件操作及字符串操作(一):
- 内容提供者访问
- Mac下配置android开发环境以及下载android源码,
- C#程序设计与应用课程教学总结:自评与改进
- java中存储区的分类
- Digital Roots
- iOS 在线播放网络资源
- gcc源代码分析,build_pointer_type ()函数分析
- Binary Tree Zigzag Level Order Traversal --- LeetCode
- HDOJ 2067 小兔的棋盘