我是靠谱客的博主 精明鲜花,最近开发中收集的这篇文章主要介绍Android学习笔记(十七),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

ContentProvider

 

DataBaseOpenHelper

package org.wp.service;
import org.wp.activity.ContentProviderMetaData;
import org.wp.activity.ContentProviderMetaData.UserTableMetaData;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DataBaseOpenHelper extends SQLiteOpenHelper {
private static final String TAG = "DataBaseOpenHelper";
private static final int VERSION = 1;
public DataBaseOpenHelper(Context context, String name) {
this(context, name, VERSION);
}
public DataBaseOpenHelper(Context context, String name, int version) {
this(context, name, null, version);
}
public DataBaseOpenHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.i(TAG, "Create Database");
db.execSQL("create table "
+ ContentProviderMetaData.DATABASE_TABLE_NAME + "("
+ UserTableMetaData._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ UserTableMetaData.USERS_NAME + " varchar(20),"
+ UserTableMetaData.USERS_AGE + " int)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.i(TAG, "Update Database");
}
}

 

ContentProviderMetaData

package org.wp.activity;
import android.net.Uri;
import android.provider.BaseColumns;
/**
* DATABASE_NAME:数据库名称
* DATABASE_VERSION:数据库版本
* DATABASE_TABLE_NAME:数据库表名
*
* CONTENT_URI:访问该ContentProvider的URI
* CONTENT_TYPE:该ContentProvider所返回的数据类型的定义
* CONTENT_TYPE_ITEM:该ContentProvider所返回的数据类型的定义
* TABLE_NAME:表名
* USERS_NAME:列名
* USERS_AGE:列名
* DEFAULT_SORT_ORDER:默认的排序方法
*
* @author wp
*
*/
public class ContentProviderMetaData {
public static final String AUTHORITY = "org.wp.activity.ContentProviderCustom";
public static final String DATABASE_NAME = "contentProviderCustom_db";
public static final Integer DATABASE_VERSION = 1;
public static final String DATABASE_TABLE_NAME = "users";
public static final class UserTableMetaData implements BaseColumns {
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/users");
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.contentprovidercustom.user";
public static final String CONTENT_TYPE_ITEM = "vnd.android.cursor.item/vnd.contentprovidercustom.user";
public static final String TABLE_NAME = "users";
public static final String USERS_NAME = "name";
public static final String USERS_AGE = "age";
public static final String DEFAULT_SORT_ORDER = "_id desc";
}
}

 

ContentProviderCustom

package org.wp.activity;
import java.util.HashMap;
import org.wp.activity.ContentProviderMetaData.UserTableMetaData;
import org.wp.service.DataBaseOpenHelper;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
public class ContentProviderCustom extends ContentProvider {
private static final String TAG = "ContentProviderCustom";
public static final UriMatcher uriMatcher;
public static final int INCOMING_USER_COLLECTION = 1;
public static final int INCOMINT_USER_SINGER = 2;
public static HashMap<String, String> userProjectionMap;
private DataBaseOpenHelper dboh;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(ContentProviderMetaData.AUTHORITY, "users",
INCOMING_USER_COLLECTION);
uriMatcher.addURI(ContentProviderMetaData.AUTHORITY, "users/#",
INCOMINT_USER_SINGER);
userProjectionMap = new HashMap<String, String>();
userProjectionMap.put(UserTableMetaData._ID, UserTableMetaData._ID);
userProjectionMap.put(UserTableMetaData.USERS_NAME,
UserTableMetaData.USERS_NAME);
userProjectionMap.put(UserTableMetaData.USERS_AGE,
UserTableMetaData.USERS_AGE);
}
/*
* 根据传入的URI,返回该URI所表示的数据类型
*/
@Override
public String getType(Uri uri) {
Log.i(TAG, "getType()");
switch (uriMatcher.match(uri)) {
case INCOMING_USER_COLLECTION:
return UserTableMetaData.CONTENT_TYPE;
case INCOMINT_USER_SINGER:
return UserTableMetaData.CONTENT_TYPE_ITEM;
default:
throw new IllegalArgumentException("Unknown URI" + uri);
}
}
/*
* onCreate()是一个回调方法,在contentProvider创建的时候执行
*/
@Override
public boolean onCreate() {
Log.i(TAG, "onCreate()");
dboh = new DataBaseOpenHelper(getContext(),
ContentProviderMetaData.DATABASE_NAME);
return true;
}
/*
* 该函数的返回值是一个Uri,这个Uri表示刚刚使用这个函数所插入的数据
* content://org.wp.activity.ContentProviderCustom/users/1
*/
@Override
public Uri insert(Uri uri, ContentValues values) {
Log.i(TAG, "insert()");
SQLiteDatabase sdb = dboh.getWritableDatabase();
Long rowId = sdb.insert(ContentProviderMetaData.DATABASE_TABLE_NAME,
null, values);
if (rowId > 0) {
Uri insertUserUri = ContentUris.withAppendedId(
UserTableMetaData.CONTENT_URI, rowId);
getContext().getContentResolver().notifyChange(insertUserUri, null);
return insertUserUri;
}
throw new SQLException("Failed to insert row into" + uri);
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
Log.i(TAG, "query()");
SQLiteQueryBuilder sqb = new SQLiteQueryBuilder();
switch (uriMatcher.match(uri)) {
case INCOMING_USER_COLLECTION:
sqb.setTables(UserTableMetaData.TABLE_NAME);
sqb.setProjectionMap(userProjectionMap);
break;
case INCOMINT_USER_SINGER:
sqb.setTables(UserTableMetaData.TABLE_NAME);
sqb.setProjectionMap(userProjectionMap);
sqb.appendWhere(UserTableMetaData._ID + "="
+ uri.getPathSegments().get(1));
break;
}
String orderBy;
if (TextUtils.isEmpty(sortOrder)) {
orderBy = UserTableMetaData.DEFAULT_SORT_ORDER;
} else {
orderBy = sortOrder;
}
SQLiteDatabase sdb = dboh.getWritableDatabase();
Cursor cur = sqb.query(sdb, projection, selection, selectionArgs, null,
null, orderBy);
cur.setNotificationUri(getContext().getContentResolver(), uri);
return cur;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
Log.i(TAG, "delete()");
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
Log.i(TAG, "update()");
return 0;
}
}

 

ContentProviderActivity

package org.wp.activity;
import org.wp.activity.ContentProviderMetaData.UserTableMetaData;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class ContentProviderActivity extends Activity {
private static final String TAG = "ContentProviderActivity";
private Button insertButton;
private Button queryButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
insertButton = (Button) this.findViewById(R.id.insert);
queryButton = (Button) this.findViewById(R.id.query);
insertButton.setOnClickListener(listener);
queryButton.setOnClickListener(listener);
Log.i(TAG, getContentResolver().getType(UserTableMetaData.CONTENT_URI));
}
private View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Button button = (Button) v;
switch (button.getId()) {
case R.id.insert:
ContentValues values = new ContentValues();
values.put(UserTableMetaData.USERS_NAME, "张三");
values.put(UserTableMetaData.USERS_AGE, 10);
Uri uri = getContentResolver().insert(
UserTableMetaData.CONTENT_URI, values);
Toast.makeText(ContentProviderActivity.this, uri.toString(),
Toast.LENGTH_LONG).show();
Log.i(TAG, uri.toString());
break;
case R.id.query:
Cursor cur = getContentResolver().query(
UserTableMetaData.CONTENT_URI, null, null, null, null);
while (cur.moveToNext()) {
Log.i(TAG, cur.getString(cur
.getColumnIndex(UserTableMetaData.USERS_NAME)));
Log.i(TAG,
cur.getInt(cur
.getColumnIndex(UserTableMetaData.USERS_AGE))
+ "");
}
break;
}
}
};
}

 

 

 

最后

以上就是精明鲜花为你收集整理的Android学习笔记(十七)的全部内容,希望文章能够帮你解决Android学习笔记(十七)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(40)

评论列表共有 0 条评论

立即
投稿
返回
顶部