當(dāng)外部應(yīng)用要對ContentProvider中的數(shù)據(jù)進行操作的時候_第1頁
當(dāng)外部應(yīng)用要對ContentProvider中的數(shù)據(jù)進行操作的時候_第2頁
當(dāng)外部應(yīng)用要對ContentProvider中的數(shù)據(jù)進行操作的時候_第3頁
當(dāng)外部應(yīng)用要對ContentProvider中的數(shù)據(jù)進行操作的時候_第4頁
當(dāng)外部應(yīng)用要對ContentProvider中的數(shù)據(jù)進行操作的時候_第5頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

1、當(dāng)外部應(yīng)用要對ContentProvider中的數(shù)據(jù)進行操作的時候,可以使用ContentResolver類來完成,要獲取ContentResolver對象,可以使用activity的getContentResolver方法。 contentResolver類提供了與contentProvider相同簽名的四個方法public Uri insert(URI uri,ContentValues values); 這個方法用于添加數(shù)據(jù)public int delete(Uri  uri,String selection,String selectionArgs) 這個方法用于刪

2、除數(shù)據(jù)public int update(Uri uri,ContentValues values,String selection,Stringp selectionArgs) 更新方法  下面用查詢方法詳細(xì)舉例 public class PersonProvider extends ContentProvider     private static final String TAG = "PersonContentProvider"    DBOpenHelper dbHelpe

3、r;       private static final int PERSONLIST = 1;       private static final int PERSON = 2;    private static UriMatcher matcher;    static       matcher = new UriMatcher(UriMatch

4、er.NO_MATCH);       matcher.addURI("vider.PersonProvider", "person", PERSONLIST);       matcher.addURI("vider.PersonProvider", "person/#", PERSON);     &#

5、160;    Override    public String getType(Uri uri)        switch (matcher.match(uri)            case PERSONLIST:/獲取所有記錄  就是這樣規(guī)定的 多條記錄返回這個        

6、      return "vnd.android.cursor.dir/personprovider.person"           case PERSON:/獲取單條記錄  單條記錄返回那個              return "vnd.android.cursor.

7、item/personprovider.person"           default:              throw new IllegalArgumentException("Uri IllegalArgument:"+ uri);        

8、60;         Override    public boolean onCreate()        dbHelper = new DBOpenHelper(this.getContext();       return true;         Override  

9、0; public Cursor query(Uri uri, String projection, String selection, String selectionArgs, String sortOrder)        /獲取數(shù)據(jù)庫操作的類       SQLiteDatabase db = dbHelper.getReadableDatabase();       /聲明一個游標(biāo)  

10、     Cursor cursor = null;       switch (matcher.match(uri)            case PERSONLIST:/獲取所有記錄              /cursor = db.query("pe

11、rson", projection, selection, selectionArgs, null, null, sortOrder);              cursor = db.rawQuery("select * from person ", null);              break;&

12、#160;          case PERSON:/獲取單條記錄              long personid = ContentUris.parseId(uri);              String where = selecti

13、on!=null && !"".equals(selection.trim() ? selection+ " and personid="+ personid : "personid="+ personid;              cursor = db.query("person", projection, where, selectionArgs, null, n

14、ull, sortOrder);              break;           default:              /如果什么都沒有找到,就拋出一個異常。              throw new IllegalArgumentException("Uri IllegalArgument:"+ uri);            

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論