4.Uri.parse(String str)方法是将字符串转化成Uri对象的。为了解析Uri对象,Android系统提供了一个辅助工具类UriMatcher 用于匹配Uri。
UriMatcher 中的常见方法如下所示:主要代码:
MainActivity.java
package com.hh.contentprovider;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private ListView lv;
private List persons;
//创建一个Handler对象用于线程间通信
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 100://接收到数据查询完毕的消息
//UI线程适配ListView
lv.setAdapter(new MyAdapter());
break;
}
};
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
//由于添加数据、查询数据是比较耗时的,因此需要在子线程中做这两个操作
new Thread(){
public void run() {
//添加数据
addData();
//获取persons集合
getPersons();
//如果查询到数据 则向UI线程发送消息
if(persons.size() > 0){
handler.sendEmptyMessage(100);
}
};
}.start();
}
// 往person表中插入10条数据
public void addData() {
PersonDao dao = new PersonDao(this);
long number = 885900000l;
Random random = new Random();
for (int i = 0; i < 10; i++) {
dao.add("wangwu" + i, Long.toString(number + i));
}
}
//利用ContentResolver对象查询本应用程序使用ContentProvider暴露的数据
private void getPersons() {
//首先要获取查询的uri
String url = "content://com.hh.contentprovider.personprovider/query";
Uri uri = Uri.parse(url);
//获取ContentResolver对象 这个对象的使用后面会详细讲解
ContentResolver contentResolver = getContentResolver();
//利用ContentResolver对象查询数据得到一个Cursor对象
Cursor cursor = contentResolver.query(uri, null, null, null, null);
persons = new ArrayList();
//如果cursor为空立即结束该方法
if(cursor == null){
return;
}
while(cursor.moveToNext()){
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String number = cursor.getString(cursor.getColumnIndex("number"));
Person p = new Person(id, name, number);
persons.add(p);
}
cursor.close();
}
//适配器
private class MyAdapter extends BaseAdapter{
private static final String TAG = "MyAdapter";
// 控制listview里面总共有多个条目
public int getCount() {
return persons.size(); //条目个数 == 集合的size
}
public Object getItem(int position) {
return persons.get(position);
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
//得到某个位置对应的person对象
Person person = persons.get(position);
View view = View.inflate(MainActivity.this, R.layout.list_item, null);
//一定要在view对象里面寻找孩子的id
//姓名
TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
tv_name.setText("姓名:"+person.getName());
//电话
TextView tv_phone = (TextView) view.findViewById(R.id.tv_phone);
tv_phone.setText("电话:"+person.getNumber());
return view;
}
}
}
Person.java
package com.hh.contentprovider;
public class Person {
private int id;
private String name;
private String number;
public Person() {
}
public String toString() {
return "Person [id=" + id + ", name=" + name + ", number=" + number
+ "]";
}
public Person(int id, String name, String number) {
this.id = id;
this.name = name;
this.number = number;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
PersonDao.java
package com.hh.contentprovider;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
public class PersonDao {
private PersonSQLiteOpenHelper helper;//创建了一个私有的helper变量
//在构造方法里面完成helper的初始化
public PersonDao(Context context){//构造方法
helper = new PersonSQLiteOpenHelper(context);
}
//添加一条记录到数据库
public long add(String name,String number){//添加内容
SQLiteDatabase db = helper.getWritableDatabase();//写入内容
ContentValues values = new ContentValues();
values.put("name", name);
values.put("number", number);
long id = db.insert("person", null, values);
db.close();
return id;
}
}
PersonDBProvider.java
package com.hh.contentprovider;
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;
import com.hh.contentprovider.PersonSQLiteOpenHelper;
public class PersonDBProvider extends ContentProvider {
// 定义一个uri的匹配器 用于匹配uri 如果路径不满足条件 返回 -1
private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
private static final int INSERT = 1; //添加数据匹配Uri路径成功时返回码
private static final int DELETE = 2; //删除数据匹配Uri路径成功时返回码
private static final int UPDATE = 3; //更改数据匹配Uri路径成功时返回码
private static final int QUERY = 4; //查询数据匹配Uri路径成功时返回码
private static final int QUERYONE = 5; //查询一条数据匹配Uri路径成功时返回码
//数据库操作类的对象
private PersonSQLiteOpenHelper helper;
static {
// 添加一组匹配规则.
matcher.addURI("com.hh.contentprovider.personprovider", "insert", INSERT);
matcher.addURI("com.hh.contentprovider.personprovider", "delete", DELETE);
matcher.addURI("com.hh.contentprovider.personprovider", "update", UPDATE);
matcher.addURI("com.hh.contentprovider.personprovider", "query", QUERY);
//这里的“#”号为通配符凡是符合”query/”皆返回QUERYONE的返回码
matcher.addURI("com.hh.contentprovider.personprovider", "query/#", QUERYONE);
}
//当内容提供者被创建的时候 调用 适合 数据的初始化
public boolean onCreate() {
helper = new PersonSQLiteOpenHelper(getContext());
return false;
}
//查询数据操作
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
if (matcher.match(uri) == QUERY) { //匹配查询的Uri路径
//匹配成功 ,返回查询的结果集
SQLiteDatabase db = helper.getReadableDatabase();
//调用数据库操作的查询数据的方法
Cursor cursor = db.query("person", projection, selection,
selectionArgs, null, null, sortOrder);
return cursor;
} else if (matcher.match(uri) == QUERYONE) {
//匹配成功,根据id查询数据
long id = ContentUris.parseId(uri);
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.query("person", projection, "id=?",
new String[]{id+""}, null, null, sortOrder);
return cursor;
} else {
throw new IllegalArgumentException("路径不匹配,不能执行查询操作");
}
}
//获取当前Uri的数据类型
public String getType(Uri uri) {
if (matcher.match(uri) == QUERY) {
// 返回查询的结果集
return "vnd.android.cursor.dir/person";
} else if (matcher.match(uri) == QUERYONE) {
return "vnd.android.cursor.item/person";
}
return null;
}
//添加数据
public Uri insert(Uri uri, ContentValues values) {
if (matcher.match(uri) == INSERT) {
//匹配成功 返回查询的结果集
SQLiteDatabase db = helper.getWritableDatabase();
db.insert("person", null, values);
} else {
throw new IllegalArgumentException("路径不匹配,不能执行插入操作");
}
return null;
}
//删除数据
public int delete(Uri uri, String selection, String[] selectionArgs) {
if (matcher.match(uri) == DELETE) {
//匹配成功 返回查询的结果集
SQLiteDatabase db = helper.getWritableDatabase();
db.delete("person", selection, selectionArgs);
} else {
throw new IllegalArgumentException("路径不匹配,不能执行删除操作");
}
return 0;
}
//更新数据
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
if (matcher.match(uri) == UPDATE) {
//匹配成功 返回查询的结果集
SQLiteDatabase db = helper.getWritableDatabase();
db.update("person", values, selection, selectionArgs);
} else {
throw new IllegalArgumentException("路径不匹配,不能执行修改操作");
}
return 0;
}
}
PersonSQLiteOpenHelper.java
package com.hh.contentprovider;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class PersonSQLiteOpenHelper extends SQLiteOpenHelper {
private static final String TAG = "PersonSQLiteOpenHelper";
// 数据库的构造方法,用来定义数据库的名称 数据库查询的结果集 数据库的版本
public PersonSQLiteOpenHelper(Context context) {
super(context, "person.db", null, 3);
}
// 数据库第一次被创建时调用的方法
public void onCreate(SQLiteDatabase db) {
//初始化数据库的表结构
db.execSQL("create table person (id integer primary key autoincrement, name varchar(20), number varchar(20)) ");
}
// 当数据库的版本号发生变化的时候调用
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.i(TAG,"数据库的版本变化了");
}
}
activity_main.xml
list_item.xml
Manifest.xml
小程序总代码: 读取联系人信息小程序下载https://download.csdn.net/download/huanhuan59/10481236