搜索

作业三


发布时间: 2022-11-24 19:13:01    浏览次数:33 次

内容:

1、contentprovider是安卓四大组件之一,请使用其方法类进行数据获取;
2、请自建一个provider,然后在另一个app中使用resolver调用这个provider。
3、本次作业请启用新项目,理论上需要两个APP进行实验。

先建一个SQLiteOpenHelper的子类:

importandroid.content.Context;
importandroid.database.sqlite.SQLiteDatabase;
importandroid.database.sqlite.SQLiteOpenHelper;
publicclassStuDbHelper extendsSQLiteOpenHelper {
privatestaticString DbName = "student.db";
publicStuDbHelper(Context context,intversion) {
super(context, DbName, null, version);
}
@Override
publicvoidonCreate(SQLiteDatabase db) {
String sql = "create table student (id integer primary key,name varchar(20),age integer)"; //在数据库中创建一张表
db.execSQL(sql);
}
@Override

publicvoidonUpgrade(SQLiteDatabase db, intoldVersion, intnewVersion) {
}
}
建立访问数据源的ContentProvider:

packagecth.android.contentprovider;
importandroid.content.ContentProvider;
importandroid.content.ContentUris;
importandroid.content.ContentValues;
importandroid.content.UriMatcher;
importandroid.database.Cursor;
importandroid.database.sqlite.SQLiteDatabase;
importandroid.net.Uri;
publicclassStuDbCP extendsContentProvider {
privatestaticfinalUriMatcher URI_MATCHER = newUriMatcher(UriMatcher.NO_MATCH);  //创建该内容提供者的匹配器
privatestaticfinalString AUTHORITY = "cth.android.contentprovider.StuDbCP";   //定义授权信息,用于获取该内容提供者的标识
privatestaticfinalString PATH = "student";                       //路径,表示访问内容提供者的具体路径,一般用表明表示访问该数据库的具体哪张表
privatestaticfinalintSTU = 1;    //设定标志位,STU表示单条信息
privatestaticfinalintSTUS = 2;   //STUS表示多条信息
static{
URI_MATCHER.addURI(AUTHORITY, PATH + "/#", STU);   //给匹配器加入匹配规则
URI_MATCHER.addURI(AUTHORITY, PATH, STUS);
}
privateStuDbHelper stuDbHepler = null;
privateSQLiteDatabase stuDb = null;
@Override
publicbooleanonCreate() {
booleanflag = false;
stuDbHepler = newStuDbHelper(getContext(),1);  //创建SQLiteOpenHelper对象,版本为1
if(stuDb != null) flag = true;
returnflag;
}
@Override
publicCursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
intflag = URI_MATCHER.match(uri);    //匹配传入的Uri
Cursor selectResult = null;
SQLiteDatabase db = stuDbHepler.getReadableDatabase();
String whereClause = null;
switch(flag) {
caseSTU:
whereClause = "id = "+ ContentUris.parseId(uri);  //如果匹配第一种方式表示后面跟了id,所以要先提取id
if(selection != null&& selection.equals("")) {
whereClause += " and "+ selection;   //把id加到where条件子句(下面几种方法此步骤作用同理)
}
selectResult = db.query("student", projection, whereClause, selectionArgs, null, null, null);
break;
caseSTUS:
selectResult = db.query("student", projection,selection,selectionArgs, null, null, null);
}
returnselectResult;
}
/*返回uri的路径扩展部分的信息,一般单个条目返回字段为vnd.android.cursor.item/对应的PATH 而多个条目的以vnd.android.cursor.dir/对应的PATH。*/
@Override
publicString getType(Uri uri) {  
intflag = URI_MATCHER.match(uri);
switch(flag) {
caseSTU:
return"vnd.android.cursor.item/student";
caseSTUS:
return"vnd.android.cursor.dir/students";
}
returnnull;
}
@Override
publicUri insert(Uri uri, ContentValues values) {
intflag = URI_MATCHER.match(uri);
Uri resultUri = null;
switch(flag) {
caseSTUS :
stuDb = stuDbHepler.getWritableDatabase();   //获取写数据库
longid = stuDb.insert(PATH, null, values);   //插入数据
resultUri = ContentUris.withAppendedId(uri, id);  //建立插入的数据的URI
break;
}
returnresultUri;
}
@Override
publicintdelete(Uri uri, String selection, String[] selectionArgs) {
intflag = URI_MATCHER.match(uri);
String whereClause = null;
introwCount = -1;
SQLiteDatabase db = stuDbHepler.getWritableDatabase();
switch(flag) {
caseSTU:
whereClause = "id = "+ ContentUris.parseId(uri);
if(selection != null&& selection.equals("")) {
whereClause += " and "+ selection ;
}
rowCount = db.delete("student", whereClause, selectionArgs);
break;
caseSTUS:
rowCount = db.delete("student", selection, selectionArgs);
default: break;
}
returnrowCount;
}
@Override
publicintupdate(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
intflag = URI_MATCHER.match(uri);
intcount = -1;
SQLiteDatabase db = stuDbHepler.getWritableDatabase();
String whereClause = null;
switch(flag) {
caseSTU:
whereClause = "id = "+ ContentUris.parseId(uri);
if(selection != null&& selection.equals("")) {
whereClause += " and "+ selection ;
}
count = db.update("student", values, whereClause, selectionArgs);
break;
caseSTUS:
count = db.update("student", values, selection, selectionArgs);
break;
}
returncount;
}
}
在Manifest文件的application标签中对其进行注册:

importandroid.content.ContentResolver;
importandroid.content.ContentValues;
importandroid.database.Cursor;
importandroid.net.Uri;
importandroid.test.AndroidTestCase;
importandroid.util.Log;
/*
* 新建一个工程,建立一个测试类来测试是否能够跨进程访问数据。分别实现增删改查四种方法。
* */
publicclassTestCP extendsAndroidTestCase {
publicvoidinsertData() {
ContentResolver cr = getContext().getContentResolver();
Uri uri = Uri.parse(" content://cth.android.contentprovider.StuDbCP/student");
ContentValues values = newContentValues();
values.put("id", 3);
values.put("name", "Jiky");
values.put("age", 18);
Uri resultUri = cr.insert(uri, values);
if(resultUri != null) {
Log.i("cth",resultUri.toString());
} else{
Log.e("cth","插入失败");
}
}
publicvoiddeleteData() {
ContentResolver cr = getContext().getContentResolver();
Uri uri = Uri.parse(" content://cth.android.contentprovider.StuDbCP/student");
String where = "id = ?";
intdeleteRowNum = cr.delete(uri, where, newString[] {"123"});
Log.i("cth","deleteRowNum is "+ deleteRowNum );
}
publicvoidupdateData() {
ContentResolver cr = getContext().getContentResolver();
Uri uri = Uri.parse(" content://cth.android.contentprovider.StuDbCP/student/"); //直接把要修改的id加在PATH后,也可按一下方式。
ContentValues values = newContentValues();
values.put("name","Mike");
values.put("age", 11);
introwId = cr.update(uri, values, "id = ?", newString[]{"1"});
if(rowId == 0) {
Log.e("cth","找不到匹配项。");
} else{
Log.i("cth","rowId = "+ rowId);
}
}
publicvoidselectData() {
ContentResolver cr = getContext().getContentResolver();
Uri uri = Uri.parse(" content://cth.android.contentprovider.StuDbCP/student/");
Cursor cursor = cr.query(uri,newString[]{"name","id"}, null, null, null);
while(cursor.moveToNext()) {
Log.i("cth",cursor.getString(cursor.getColumnIndex("id")) + " count = "+ cursor.getCount());
}
}
}

 

 
免责声明 作业三,资源类别:文本, 浏览次数:33 次, 文件大小:-- , 由本站蜘蛛搜索收录2022-11-24 07:13:01。此页面由程序自动采集,只作交流和学习使用,本站不储存任何资源文件,如有侵权内容请联系我们举报删除, 感谢您对本站的支持。 原文链接:https://www.cnblogs.com/sjr123456/p/16922931.html