这一章的话主要就学习了安卓四大组件之一 -- ContentProvider的基本用法。
知识点统计:
1.运行时权限 :当需要该权限时,询问用户是否允许使用。(不要忘记在AndroidManifest里声明权限)
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
switch (requestCode){
case 1:
//允许
if(grantResults.length > 0 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED){
call();
}
//拒绝
else {
Toast.makeText(this,"You denied the permission"
,Toast.LENGTH_SHORT).show();
}
break;
default:
}
}
2.ContentResolver的CRUD操作
(1)添加操作:
ContentValues values = new ContentValues();
values.put("name","A Clash Of Kings");
values.put("author","George Martin");
values.put("pages",1040);
values.put("price",55.55);
Uri newUri = getContentResolver().insert(uri,values);
(2)更新操作:
ContentValues values = new ContentValues();
values.put("name","A Storm of Swords");
values.put("pages",1216);
values.put("price",24.05);
getContentResolver().update(uri,values,null,null);
(3)删除操作:
getContentResolver().delete(uri,null,null);
(4)查询操作:
Cursor cursor = getContentResolver().query(uri,null,null
,null,null);
if(cursor != null){
while (cursor.moveToNext()){
String name = cursor.getString(
cursor.getColumnIndex("name"));
String author = cursor.getString(cursor
.getColumnIndex("author"));
int pages = cursor.getInt(cursor
.getColumnIndex("pages"));
double price = cursor.getDouble(cursor.getColumnIndex
("price"));
Log.d(TAG, "Name: "+name);
Log.d(TAG, "Author: "+author);
Log.d(TAG, "Pages: "+pages);
Log.d(TAG, "Price: "+price);
}
cursor.close();
}
3.创建内容提供器步骤
(1)创建使用UriMatcher匹配Uri
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY,"book",BOOK_DIR);
uriMatcher.addURI(AUTHORITY,"book/#",BOOK_ITEM);
uriMatcher.addURI(AUTHORITY,"category",CATEGORY_DIR);
uriMatcher.addURI(AUTHORITY,"category/#",CATEGORY_ITEM);
}
(2)重写六种方法
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// Implement this to handle requests to delete one or more rows.
......
}
@Override
public String getType(Uri uri) {
// TODO: Implement this to handle requests for the MIME type of the data
// at the given URI.
switch (uriMatcher.match(uri)){
case BOOK_DIR:
return "vnd.android.cursor.dir/vnd.com.example." +
"databasetest.provider.book";
case BOOK_ITEM:
return "vnd.android.cursor.item/vnd.com.example." +
"databasetest.provider.book";
case CATEGORY_DIR:
return "vnd.android.cursor.dir/vnd.com.example." +
"databasetest.provider.category";
case CATEGORY_ITEM:
return "vnd.android.cursor.item/vnd.com.example." +
"databasetest.provider.category";
default:
break;
}
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO: Implement this to handle requests to insert a new row.
......
}
@Override
public boolean onCreate() {
// TODO: Implement this to initialize your content provider on startup.
dbHelper = new MyDatabaseHelper(getContext(),"BookStore.db"
,null,1);
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO: Implement this to handle query requests from clients.
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = null;
switch (uriMatcher.match(uri)){
case BOOK_DIR:
cursor = db.query("Book",projection,selection,selectionArgs,
null,null,sortOrder);
break;
case BOOK_ITEM:
String bookId = uri.getPathSegments().get(1);
//[0]:路径 [1]:id
cursor = db.query("Book",projection,"id = ?",new String[]{bookId}
,null,null,sortOrder);
break;
case CATEGORY_DIR:
cursor = db.query("Category",projection,selection,selectionArgs,
null,null,sortOrder);
break;
case CATEGORY_ITEM:
String categoryId = uri.getPathSegments().get(1);
cursor = db.query("Category",projection,"id = ?"
,new String[]{categoryId},null,null,sortOrder);
break;
default:
break;
}
return cursor;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO: Implement this to handle requests to update one or more rows.
......
}
4.使用提供数据的接口
使用接口非常简单,只需将唯一标示uri传入即可使用
这里给出上述query方法的使用代码:
Button queryData = (Button)findViewById(R.id.query_data);
queryData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("content://com.example.databasetest.provider/book");
Cursor cursor = getContentResolver().query(uri,null,null
,null,null);
if(cursor != null){
while (cursor.moveToNext()){
String name = cursor.getString(
cursor.getColumnIndex("name"));
String author = cursor.getString(cursor
.getColumnIndex("author"));
int pages = cursor.getInt(cursor
.getColumnIndex("pages"));
double price = cursor.getDouble(cursor.getColumnIndex
("price"));
Log.d(TAG, "Name: "+name);
Log.d(TAG, "Author: "+author);
Log.d(TAG, "Pages: "+pages);
Log.d(TAG, "Price: "+price);
}
cursor.close();
}
}
});
5.Git使用
(书籍在这里又出现了Git的知识点,暂且记下,以后再规整)
(1) git add . ,添加所有文件
(2) git commit -m “本次提交的描述” ,提交添加操作
(3) git status ,查看文件修改情况
(4) git diff (+文件路径) ,查看修改具体内容
(5) git checkout +文件路径 ,撤销修改
(6) git reset HEAD +文件路径 ,取消文件添加
(7) git log ,显示提交记录
(8) git log +记录id -1 ,选中该id记录并只显示一行记录
6.小结
这是我第一次真正意义上的对自己学习过程的一次记录,将内容提供器我认为比较重要的内容都整理记录了下来,以后会坚持记录下去。
经验不足,多多见谅。