ContentProvider初步——————-筆記

來源:互聯網
上載者:User

Provider

查閱文檔,重點功能是在不同的應用程式之間共用資料,很少去實現它,大部分是使用android內部的Content
provider(一)基本概念(1)為儲存或擷取資料提供了統一的介面(2)可以在不同的應用程式之間共用資料(3)android 為常見的一些資料提供了 ContentProvider(包括音頻圖片動畫通訊錄等)URI為每一個ContentProvider所共有,用於表示ContentProvider所提供的資料,一個應用對應一個ContentProvider,外部通過CONTENT_URI來擷取自身提供的資料(資料模型ContentProvider使用表的形式來組織資料)ContentProvider提供的函數:query,insert,update,delete,getType,onCreate(二)實現ContentProvider過程:(1)定義CONTENT_URI常量必須是唯一的 public static final Uri CONTENT_URI =
Uri.parse("content://帶包名的完整的類名");(要是存在很多表應為每一個子表定義一個CONTENT_URI,並且表中必須存在一個名為_id的列)資料類型讀取整個表中的資料:"vnd.android.cursor.dir/名字"、讀取某一個列的資料:"vnd.android.cursor.item/名字"(2)定義一個類,繼承ContentProvider在其中定義一個UriMatcher對象用於檢查URI是否合法,具體實現參看Contact類的ContentPrivider,根據其實現方法自訂一個檢查機制(3)實現query,insert,update,delete,getType,onCreate方法uriMatcher.match(uri)返回相應的int值insert方法:參數: Uri uri ,ContentValues values(索引值對)傳回值:Uri(表示幹剛使用這個函數所插入的資料)dh = new
DataBaseHelper(getContext(),FirstProviderMetaData.DATABASE_NAME)SQLiteDatabase db =
dh.getWriteDatabase();//獲得可寫的SQLite資料庫long rowId =
db.insert(UserTableMetaData.TABLE_NAME,null,values)//執行查詢方法,返回剛插入記錄的IDUri uri =
ContentUris.withAppendId(UserTableMetaData.CONTENT_URI,rowId);//把新產生的ID追加到ContentURI後面getContext().getContentResolver.notifyChange(insertedUserUri,null);//通知監聽器資料已經改變return  uriquery方法:參數Uri,projection,selection,selectionArgs,oderBy傳回值:Cursor對象SQLiteQueryBuilder qb = new
SQLiteQueryBuilder();//使用SQLiteQueryBuilder類建立一個查詢的語句qb.setTables(表名);qb.setProjectionMap(HashMap對象);//將事先添加好的索引值對添加到qb子句中qb.appendWhere(where子句)(尋找函數uri.getPathSegments()的具體功能)Cursor c =
qb.query(db,projection,selection,selectionArgs,null,null,oderBy)c.setNotificationUri(getContext.getContentResolver(),uri);//通知監聽器資料已經查詢成功(4)在AndroidManifest.xml中進行聲明<provider android:name="類名全稱"android:authorities="URI中間的字串"/>URI中間的字串:如content://authority/名稱中的authority

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.