Content Providers--怎样去访问一个基本的content provider
来源:互联网 发布:编小说的软件 编辑:程序博客网 时间:2024/04/30 03:54
一个content provider 管理着怎样去访问存储在应用仓库中的数据。一个content provider是一个应用程序的一部分,它常常用来使应用程序的界面工作在它提供的数据基础上。然而,content providers 主要是用来给其它的应用程序使用的,使用一个provider客户端对象来访问使用它。总之,providers和provider 客户端对象为应用中的数据提供一个一致的、标准的访问接口,并且它也能够处理跨进程间的通信和敏感数据的访问
Accessing a provider
我们可以使用
ContentResolver
客户端对象去访问一个content provider,我们可以用 ContentResolver
实现对content provider提供的数据源进行CRUDContentResolver
对象是运行在应用的主线程中的,有可能会阻塞主线程,它不用做过多的操作就可以进行跨进程间的通信,它担当着本身仓库数据和外部表数据的沟通者当访问一个provider时,你需要在manifest文件中增加Content Provider Permissions权限声明
我们可以使用URI去定位一个content provider,使用SQL语句描述我们要进行的操作
data as table words
word app id frequency locale _ID mapreduceuser1100en_US1precompileruser14200fr_FR2appletuser2225fr_CA3constuser1255pt_BR4intuser5100en_UK5
查询操作
当我们进行查询时,并不像我们用一个url去定位一个资源时那样,一股脑的把所有的数据都返回回来,而是返回一个Cursor对象,让我们可以更明智的获取数据
SQL : select _ID,word,locale from words where word = ?
private void accessContract(String mSearchString){ Log. e(TAG, mSearchString); //uri 定位 Uri uri = UserDictionary.Words. CONTENT_URI; //access a single row in a table //Uri singleUri = ContentUris.withAppendedId(UserDictionary.Words.CONTENT_URI, 3); // A "projection" defines the columns that will be returned for each row // 要查询的字段 String[] mProjection = { UserDictionary.Words. _ID, // Contract class constant for the _ID column name UserDictionary.Words. WORD, // Contract class constant for the word column name UserDictionary.Words. LOCALE // Contract class constant for the locale column name }; // Defines a string to contain the selection clause // 查询条件 String mSelectionClause = null; // Initializes an array to contain selection arguments // 查询条件的值 String[] mSelectionArgs = { ""}; // Remember to insert code here to check for invalid or malicious input. // If the word is the empty string, gets everything if (TextUtils.isEmpty(mSearchString)) { // Setting the selection clause to null will return all words mSelectionClause = null; mSelectionArgs[0] = ""; } else { // Constructs a selection clause that matches the word that the user entered. mSelectionClause = UserDictionary.Words. WORD + " = ?" ; // Moves the user's input string to the selection arguments. mSelectionArgs[0] = mSearchString; } // Queries the user dictionary and returns results Cursor mCursor = getContentResolver().query( uri, // The content URI of the words table mProjection, // The columns to return for each row mSelectionClause, // Selection criteria //null, mSelectionArgs, // Selection criteria //null, UserDictionary.Words. WORD); // The sort order for the returned rows //last generate sql //SELECT _ID, word, frequency, locale FROM words WHERE word = <userinput> ORDER BY word ASC; // Some providers return null if an error occurs, others throw an exception if (null == mCursor) { /* * Insert code here to handle the error. Be sure not to use the cursor! You may want to * call android.util.Log.e() to log this error. * */ // If the Cursor is empty, the provider found no matches Log. e(TAG, "mCursor is null"); } else if (mCursor.getCount() < 1) { /* * Insert code here to notify the user that the search was unsuccessful. This isn't necessarily * an error. You may want to offer the user the option to insert a new row, or re-type the * search term. */ Log. e(TAG, "mCursor is empty"); } else { // Insert code here to do something with the results Log. e(TAG, "mCursor is not empty"); //display(mCursor); } //mCursor.close(); }
插入操作
用ContentValues封装要插入的字段和值
SQL: insert into words(_ID,locale,word,frequency) values (?,?,?,?)
private void insert(){ // Defines a new Uri object that receives the result of the insertion Uri mNewUri; //... // Defines an object to contain the new values to insert ContentValues mNewValues = new ContentValues(); /* * Sets the values of each column and inserts the word. The arguments to the "put" * method are "column name" and "value" */ mNewValues.put(UserDictionary.Words. APP_ID, "example.user" ); mNewValues.put(UserDictionary.Words. LOCALE, "en_US" ); mNewValues.put(UserDictionary.Words. WORD, "insert" ); mNewValues.put(UserDictionary.Words. FREQUENCY, "100" ); mNewUri = getContentResolver().insert( UserDictionary.Words. CONTENT_URI, // the user dictionary content URI mNewValues // the values to insert ); Log. e(TAG, mNewUri.getPath()); }
更新操作
SQL: update words set locale=? where locale=?
private void update(){ // Defines an object to contain the updated values ContentValues mUpdateValues = new ContentValues(); // Defines selection criteria for the rows you want to update String mSelectionClause = UserDictionary.Words.LOCALE + " LIKE ?"; String[] mSelectionArgs = { "en_%"}; // Defines a variable to contain the number of updated rows int mRowsUpdated = 0; //... /* * Sets the updated value and updates the selected words. */ mUpdateValues.putNull(UserDictionary.Words. LOCALE); mRowsUpdated = getContentResolver().update( UserDictionary.Words. CONTENT_URI, // the user dictionary content URI mUpdateValues, // the columns to update mSelectionClause, // the column to select on mSelectionArgs // the value to compare to ); Log. e(TAG, "effect row "+mRowsUpdated); }
删除操作
SQL:delete from words where word=?
private void delete(){ // Defines selection criteria for the rows you want to delete String mSelectionClause = UserDictionary.Words. WORD + " = ?" ; String[] mSelectionArgs = { "insert"}; // Defines a variable to contain the number of rows deleted int mRowsDeleted = 0; //... // Deletes the words that match the selection criteria mRowsDeleted = getContentResolver().delete( UserDictionary.Words. CONTENT_URI, // the user dictionary content URI mSelectionClause, // the column to select on mSelectionArgs // the value to compare to ); Log. e(TAG, "delete effect row "+mRowsDeleted); }
使用SimpleCursorAdapter方便的来显示一个数据源数据
private void display(Cursor mCursor) { // Defines a list of columns to retrieve from the Cursor and load into an output row String[] mWordListColumns = { UserDictionary.Words. WORD, // Contract class constant containing the word column name UserDictionary.Words. LOCALE // Contract class constant containing the locale column name }; // Defines a list of View IDs that will receive the Cursor columns for each row int[] mWordListItems = { R.id.dictWord, R.id.locale}; // Creates a new SimpleCursorAdapter SimpleCursorAdapter mCursorAdapter = new SimpleCursorAdapter( getApplicationContext(), // The application's Context object R.layout. wordlist_item, // A layout in XML for one row in the ListView mCursor, // The result from the query mWordListColumns, // A string array of column names in the cursor mWordListItems // An integer array of view IDs in the row layout ); // Flags (usually none are needed) // Sets the adapter for the ListView mWordList.setAdapter(mCursorAdapter); }
- Content Providers--怎样去访问一个基本的content provider
- content provider的使用(15章-Content Providers)
- Content Providers
- Content Providers
- Content Providers
- Content Providers
- Content Providers
- Content Providers
- Content Providers
- Content Providers
- Content Providers
- Content Providers
- Content Providers
- Content Providers
- Content Providers
- content provider对联系人的基本操作
- 【Content Provider】一个完整的ContentProvider框架
- content provider 的使用
- JQuery Mobile 页面过渡效果设置
- Android编程获取网络连接状态(3G/Wifi)及调用网络配置界面
- C/C++ 笔试、面试题30
- 服务器所有端口号作用详解
- android 使用广播监听网络状态
- Content Providers--怎样去访问一个基本的content provider
- 优化数据库前 问自己10个问题
- vista 或 win7下 新移动硬盘无法创建扩展分区解决办法
- 让我们离线吧
- INIT_DELAYED_WORK() 函数剖析 (2013-01-07 15:45:11)
- cocos2d-x游戏开发(一)开始菜单
- 浅谈C#中运算符重载的方方面面
- “路漫漫其修远兮,吾将上下而求索”,方向、方向、方向在哪里?
- shell: find命令的使用方法