日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android contentprovider作用,Android ContentProvider基本使用

發布時間:2023/12/31 Android 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android contentprovider作用,Android ContentProvider基本使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、基本概念:

1.ContentProvider為存儲和獲取數據提供了統一的接口;

2.使用ContentProvider可以在不同的應用程序之間共享數據;

3.Android為常見的一些數據提供了ContentProvider(包括音頻、視頻、圖片和通訊錄等等 )

直接貼下代碼:

定義數據庫幫助類:

public class DbOpenHelper extends SQLiteOpenHelper {

//數據庫名稱

private static final String DATA_BASE_NAME = "people.db";

//數據庫版本號

private static final int DATE_BASE_VERSION = 1;

//表名-男孩

public static final String BOY_TABLE_NAME = "boy";

//表名-女孩

public static final String GIRL_TABLE_NAME = "girl";

//創建表-男孩(兩列:主鍵自增長、姓名)

private final String CREATE_BOY_TABLE = "create table " + BOY_TABLE_NAME + "(_id integer primary key autoincrement, name text)";

//創建表-女孩(兩列:主鍵自增長、姓名)

private final String CREATE_GIRL_TABLE = "create table " + GIRL_TABLE_NAME + "(_id integer primary key autoincrement, name text)";

public DbOpenHelper(Context context) {

super(context, DATA_BASE_NAME, null, DATE_BASE_VERSION);

}

@Override

public void onCreate(SQLiteDatabase db) {

db.execSQL(CREATE_BOY_TABLE);//創建男孩表

db.execSQL(CREATE_GIRL_TABLE);//創建女孩表

}

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

//一些數據庫升級操作

}

}

內容提供者:

public class MyFirstContentProvider extends ContentProvider {

private Context context;

private SQLiteDatabase sqLiteDatabase;

public static final String AUTHORITY = "com.zyc.hezuo.contentproviderdemo.MyFirstContentProvider";

public static final int BOY_URI_CODE = 0;

public static final int GIRL_URI_CODE = 1;

private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

static {

uriMatcher.addURI(AUTHORITY, DbOpenHelper.BOY_TABLE_NAME, BOY_URI_CODE);

uriMatcher.addURI(AUTHORITY, DbOpenHelper.GIRL_TABLE_NAME, GIRL_URI_CODE);

}

/**

* 獲取表名

* @param uri

* @return

*/

private String getTableName(Uri uri) {

String tableName = null;

switch (uriMatcher.match(uri)) {

case BOY_URI_CODE:

tableName = DbOpenHelper.BOY_TABLE_NAME;

break;

case GIRL_URI_CODE:

tableName = DbOpenHelper.GIRL_TABLE_NAME;

break;

}

return tableName;

}

@Override

public boolean onCreate() {

context = getContext();

initProviderData();

return false;

}

//初始化原始數據

private void initProviderData() {

sqLiteDatabase = new DbOpenHelper(context).getWritableDatabase();

sqLiteDatabase.beginTransaction();

ContentValues contentValues = new ContentValues();

contentValues.put("name", "男孩1");

sqLiteDatabase.insert(DbOpenHelper.BOY_TABLE_NAME, null, contentValues);

contentValues.put("name", "男孩2");

sqLiteDatabase.insert(DbOpenHelper.BOY_TABLE_NAME, null, contentValues);

contentValues.put("name", "男孩3");

sqLiteDatabase.insert(DbOpenHelper.BOY_TABLE_NAME, null, contentValues);

contentValues.clear();

contentValues.put("name", "女孩1");

sqLiteDatabase.insert(DbOpenHelper.GIRL_TABLE_NAME, null, contentValues);

contentValues.put("name", "女孩2");

sqLiteDatabase.insert(DbOpenHelper.GIRL_TABLE_NAME, null, contentValues);

contentValues.put("name", "女孩3");

sqLiteDatabase.insert(DbOpenHelper.GIRL_TABLE_NAME, null, contentValues);

sqLiteDatabase.setTransactionSuccessful();

sqLiteDatabase.endTransaction();

}

@Nullable

@Override

public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {

String tableName = getTableName(uri);

if (TextUtils.isEmpty(tableName)) {

throw new IllegalArgumentException("Unsupported URI:" + uri);

}

return sqLiteDatabase.query(tableName, projection, selection, selectionArgs, null, null, sortOrder);

}

@Nullable

@Override

public String getType(@NonNull Uri uri) {

return null;

}

@Nullable

@Override

public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {

String tableName = getTableName(uri);

if(TextUtils.isEmpty(tableName)){

throw new IllegalArgumentException("Unsupported URI:" + uri);

}

sqLiteDatabase.insert(tableName, null, values);

context.getContentResolver().notifyChange(uri, null);

return uri;

}

@Override

public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {

String tableName = getTableName(uri);

if (TextUtils.isEmpty(tableName)) {

throw new IllegalArgumentException("Unsupported URI:" + uri);

}

int count = sqLiteDatabase.delete(tableName, selection, selectionArgs);

if (count > 0) {

context.getContentResolver().notifyChange(uri, null);

}

return count;

}

@Override

public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {

String tableName = getTableName(uri);

if (TextUtils.isEmpty(tableName)) {

throw new IllegalArgumentException("Unsupported URI:" + uri);

}

int row = sqLiteDatabase.update(tableName, values, selection, selectionArgs);

if (row > 0) {

context.getContentResolver().notifyChange(uri, null);

}

return row;

}

}

注冊內容提供者:

android:name=".MyFirstContentProvider"

android:exported="true"

android:authorities="com.zyc.hezuo.contentproviderdemo.MyFirstContentProvider" />

使用:

Uri boyUri = Uri.parse("content://com.zyc.hezuo.contentproviderdemo.MyFirstContentProvider/boy");

ContentValues contentValues = new ContentValues();

contentValues.put("name", "張三");

//getContentResolver().insert(boyUri, contentValues);

//getContentResolver().delete(boyUri, )

Cursor boyCursor = getContentResolver().query(boyUri, new String[]{"_id", "name"}, null, null, null);

if (boyCursor != null) {

while (boyCursor.moveToNext()) {

Log.e("yunchong", "ID:" + boyCursor.getInt(boyCursor.getColumnIndex("_id")) + " name:" + boyCursor.getString(boyCursor.getColumnIndex("name")));

}

boyCursor.close();

}

Uri girlUri = Uri.parse("content://com.zyc.hezuo.contentproviderdemo.MyFirstContentProvider/girl");

contentValues.clear();

//contentValues.put("name", "李四");

//getContentResolver().insert(girlUri, contentValues);

Cursor girlCursor = getContentResolver().query(girlUri, new String[]{"_id", "name"}, null, null, null);

if (girlCursor != null) {

while (girlCursor.moveToNext()) {

Log.e("yunchong", "ID:" + girlCursor.getInt(girlCursor.getColumnIndex("_id"))

+ " name:" + girlCursor.getString(girlCursor.getColumnIndex("name")));

}

girlCursor.close();

}

由于時間比較倉促,后續會繼續更新!

總結

以上是生活随笔為你收集整理的android contentprovider作用,Android ContentProvider基本使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。