ContentProvider是什么
ContentProvider是Android中的四大组件之一,主要用于不用应用之间共享数据,通过ContentProvider把应用中的数据共享给其他应用访问,其他应用可以通过ContentProvider对指定应用中的数据进行操作
ContentResolver是什么
-ContentResolver意思是内容解析者,当需要对ContentProvider中的数据进行添加、删除、修改和查询操作时,就可以使用ContentResolver类来完成,通过Context对象中的getContentResolver()方法获取该类的实例
URI是什么,标准格式是什么
Uri指定了将要操作的ContentProvider,其实可以把Uri看成网址,第一部分是”content://”,类似网址于网址的”http://”,第二部分是主机名“com.example.ContentTest”,用于唯一标识这个ContentProvider,第三部分是路径名”/student”,用来表示将要操作的数据。
“content://com.example.ContentTest/student”
1
UriMatcher是什么
Uri代表了要操作的ContentProvider,那我们怎么解析Uri,并从Uri中获取数据呢。Android系统给我们提供了两个用于操作Uri的工具类,分别为UriMatcher和ContentUris
UriMatcher本质是一个文本过滤器,使用UriMatcher就可以轻松实现匹配URI的功能,UriMatcher中提供了一个addURI()方法,第一个参数是就是主机名”content://com.example.ContentTest”,第二个参数是路径,第三个参数是匹配码
uriMatcher.addURI(“content://com.example.ContentTest”, “student”, 1);
1
如何使用ContentProvider
创建一个ContentTest类继承ContentProvider,并实现里面的6个方法
在onCreate()方法中获取数据库的对象,以及UriMatcher
@Override
public boolean onCreate() {
Log.e(TAG, “onCreate: “);
studentSql = new StudentSql(getContext(), “Student.db”, null, 1);
db = studentSql.getWritableDatabase();
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(“content://com.example.ContentTest”, “student”, 1);
uriMatcher.addURI(“content://com.example.ContentTest”, “aaaaaaa”, 2);
return false;
}
然后在增删改查四个方法中添加逻辑,通过uriMatcher.match(uri)获得匹配码
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
Log.e(TAG, “query: “);
Cursor cursor = null;
switch (uriMatcher.match(uri)) {
case 1:
cursor = db.query(“student”, null, null, null, null, null, null);
break;
case 2:
break;
default:
break;
}
return cursor;
}
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
Log.e(TAG, "insert: ");
switch (uriMatcher.match(uri)) {
case 1:
db.insert("student", null, values);
break;
case 2:
break;
default:
break;
}
return null;
}
@Override
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
Log.e(TAG, "delete: ");
switch (uriMatcher.match(uri)) {
case 1:
db.delete("student", selection, selectionArgs);
break;
case 2:
break;
default:
break;
}
return 0;
}
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
Log.e(TAG, "update: ");
switch (uriMatcher.match(uri)) {
case 1:
db.update("student", values, selection, selectionArgs);
break;
case 2:
break;
default:
break;
}
return 0;
}
之后在Activity中添加逻辑,获取Uri
uri = Uri.parse(“content://com.example.ContentTest/student”);
contentResolver = getContentResolver();
在点击事件中使用ContentResolver对ContentProvider中的数据进行添加、删除、修改和查询操作
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.insert:
values.clear();
name = studentName.getText().toString();
id = Integer.parseInt(studentID.getText().toString());
values.put(“name”, name);
values.put(“studentID”, id);
contentResolver.insert(uri,values);
break;
case R.id.update:
values.clear();
name = studentName.getText().toString();
id = Integer.parseInt(studentID.getText().toString());
values.put(“name”, name);
values.put(“studentID”, id);
contentResolver.update(uri,values,”name = ?”, new String[]{name});
break;
case R.id.delete:
name = studentName.getText().toString();
contentResolver.delete(uri, “name = ?”,new String[]{name});
break;
case R.id.query:
List list = new ArrayList<>();
Cursor cursor = contentResolver.query(uri,null,null,null,null);
if (cursor.moveToFirst()) {
do {
StudentBean studentBean = new StudentBean(cursor.getString(cursor.getColumnIndex(“name”)),
cursor.getInt(cursor.getColumnIndex(“studentID”)));
list.add(studentBean);
} while (cursor.moveToNext());
}
studentAdpaater = new StudentAdpaater(this,list);
listView.setAdapter(studentAdpaater);
break;
default:
break;
}
}