Android四大組件之——ContentProvider(二)

來源:互聯網
上載者:User

標籤:des   android   style   blog   http   color   io   os   ar   

Content Resolver介紹:

   開發人員文檔中這麼定義的:

This class provides applications access to the content model.

這個類為應用提供訪問Content模型的功能。

Content Resolver是我們應用裡單一全域執行個體,為我們訪問我們自己的應用或其他應用的Content Provider。就如同名字所描述的:Content Resolver接收來自客戶的請求,然後解決它們的請求,通過將請求指向特定主機名稱的Content Provider解決。

 

Content Resolver包括了CRUD方法(create,read,update,delete)。這些正好與Content Provider中的抽象方法

                 (insert ,query,update,delete)一 一對應。

                 (圖轉自http://www.cnblogs.com/plokmju/p/android_ContentProvider.html)

 

Content Resolver不知道Content Provider是怎樣對資料操作的,也不需要知道。Content Resolver的每個方法通過傳遞

URI到特定的Content Provider來實現對資料產生影響的操作。

 下面我將通過一個Content Resolver的Demo實現對Android系統的User Dictionary進行增刪改查操作。

  增加(insert):

 

//調用getContentResolver()擷取ContentResolver對象ContentResolver contentResolver = getContentResolver;ContentValues values = new ContentValues();values.put(Words.WORD,"NewWord");     //調用ContentResolver.insert(Uri url, ContentValues values)方法增加資料contentResolver.insert(UserDictionary.Words.CONTENT_URI, values);

 

刪除(delete):

//調用ContentResolver.delete(Uri url, String where, String[] selectionArgs)//方法刪除資料,返回的是刪除的行數long deleted = cr.delete(Words.CONTENT_URI,Words.WORD + "= ?", new String[]{"NewWord"});

尋找(query):

String [] projection = new String[]{                BaseColumns._ID,                UserDictionary.Words.WORD        };Cursor cursor =cr                .query(UserDictionary.Words.CONTENT_URI,                        projection,                         null,                        null,                        null); if(cursor.moveToFirst()){            do{                long id = cursor.getLong(0);                String word= cursor.getString(1);                Map<String,Object>map = new HashMap<String,Object>();                                map.put("id", id);                map.put("word", word);                list.add(map);            }while(cursor.moveToNext());

修改(update):

//調用ContentResolver.update(Uri uri, ContentValues values, String where, //String[] selectionArgs)方法更新資料//返回更新行數的個數long update = cr.update(uri, values, null, null);

 

應用:                                                                                       使用者字典:

                        

  本人郵箱:JohnTsai.Work@gmail.com,歡迎交流討論。      

  歡迎轉載,轉載請註明網址:http://www.cnblogs.com/JohnTsai

Android四大組件之——ContentProvider(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.