WEB开发网
开发学院手机开发Android 开发 Android 数据存储之 ContentProvider 阅读

Android 数据存储之 ContentProvider

 2010-03-03 16:44:00 来源:WEB开发网   
核心提示:case CONTACT:// 非集合类型必须在前面加上vnd.android.cursor.item/return "vnd.android.cursor.item/contact";default:throw new IllegalArgumentException("Uri Ille

case CONTACT:// 非集合类型必须在前面加上vnd.android.cursor.item/

return "vnd.android.cursor.item/contact";

default:

throw new IllegalArgumentException("Uri IllegalArgument:" + uri);

}

}

@Override

public Uri insert(Uri uri, ContentValues values) {

SQLiteDatabase db = this.openHelper.getWritableDatabase();

long id;

switch (uriMatcher.match(uri)) {

case CONTACT_LIST:

// 因为后台需要生成SQL语句,当values为null时,必须提第二个参数。生成的SQL语句才不会出错!

id = db.insert(tableName, "_id", values);

return ContentUris.withAppendedId(uri, id);

case CONTACT:

id = db.insert(tableName, "_id", values);

String uriPath = uri.toString();

String path = uriPath.substring(0, uriPath.lastIndexOf("/")) + id;

return Uri.parse(path);

default:

throw new IllegalArgumentException("Uri IllegalArgument:" + uri);

}

}

@Override

public boolean onCreate() {

this.openHelper = new MyOpenHelper(this.getContext());

return true;

}

@Override

public Cursor query(Uri uri, String[] projection, String selection,

String[] selectionArgs, String sortOrder) {

SQLiteDatabase db = this.openHelper.getWritableDatabase();

switch (uriMatcher.match(uri)) {

case CONTACT_LIST:

return db.query(tableName, projection, selection, selectionArgs,

null, null, sortOrder);

case CONTACT:

long id = ContentUris.parseId(uri);

String where = "_id=" + id;

if (selection != null && !"".equals(selection)) {

where = where + " and " + selection;

}

return db.query(tableName, projection, where, selectionArgs, null,

上一页  1 2 3 4 5 6  下一页

Tags:Android 数据 存储

编辑录入:coldstar [复制链接] [打 印]
赞助商链接