android developer tiny share-20170303
来源:互联网 发布:淘宝店铺不存在 退货 编辑:程序博客网 时间:2024/06/04 19:19
今天继续讲android ContentProvider,讲构建查询,涉及Projection、SelectionClause、SelectionArgs、SortOrder。
下面是android developer官方的讲解:
构建查询
从提供程序中检索数据的下一步是构建查询。第一个代码段定义某些用于访问用户字典提供程序的变量:
// A "projection" defines the columns that will be returned for each rowString[] 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 clauseString mSelectionClause = null;// Initializes an array to contain selection argumentsString[] mSelectionArgs = {""};
下一个代码段以用户字典提供程序为例,显示了如何使用 ContentResolver.query()。 提供程序客户端查询与 SQL 查询类似,并且包含一组要返回的列、一组选择条件和排序顺序。
查询应该返回的列集被称为投影(变量 mProjection)。
用于指定要检索的行的表达式分割为选择子句和选择参数。 选择子句是逻辑和布尔表达式、列名称和值(变量 mSelectionClause)的组合。 如果您指定了可替换参数 ? 而非值,则查询方法会从选择参数数组(变量 mSelectionArgs)中检索值。
在下一个代码段中,如果用户未输入字词,则选择子句将设置为 null,而且查询会返回提供程序中的所有字词。 如果用户输入了字词,选择子句将设置为 UserDictionary.Words.WORD + " = ?" 且选择参数数组的第一个元素将设置为用户输入的字词。
/* * This defines a one-element String array to contain the selection argument. */String[] mSelectionArgs = {""};// Gets a word from the UImSearchString = mSearchWord.getText().toString();// Remember to insert code here to check for invalid or malicious input.// If the word is the empty string, gets everythingif (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;}// Does a query against the table and returns a Cursor objectmCursor = getContentResolver().query( UserDictionary.Words.CONTENT_URI, // The content URI of the words table mProjection, // The columns to return for each row mSelectionClause // Either null, or the word the user entered mSelectionArgs, // Either empty, or the string the user entered mSortOrder); // The sort order for the returned rows// Some providers return null if an error occurs, others throw an exceptionif (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} 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. */} else { // Insert code here to do something with the results}
此查询与 SQL 语句相似:
SELECT _ID, word, locale FROM words WHERE word = <userinput> ORDER BY word ASC;
在此 SQL 语句中,会使用实际的列名称而非协定类常量。
0 0
- android developer tiny share-20170303
- android developer tiny share-20160627
- android developer tiny share-20160628
- android developer tiny share-20160629
- android developer tiny share-20160630
- android developer tiny share-20160701
- android developer tiny share-20160704
- android developer tiny share-20160705
- android developer tiny share-20160706
- android developer tiny share-20160707
- android developer tiny share-20160708
- android developer tiny share-20160711
- android developer tiny share-20160712
- android developer tiny share-20160713
- android developer tiny share-20160714
- android developer tiny share-20160715
- android developer tiny share-20160718
- android developer tiny share-20160719
- Elasticsearch教程
- error: SEH exception with code 0xc0000005 thrown in the test
- PHP通过Http Post请求发送Json对象数据?
- 【限时免费体验】D3引擎:“拖拉拽”轻松完成数据处理开发
- 算年龄
- android developer tiny share-20170303
- iOS framework Required与Optional
- Java String.Format() 方法及参数说明
- IDAPython类库---idc.py的源码
- Spring-Boot学习笔记-整合Mybatis-Druid-PageHelper
- React依赖注入说明(mapStateToProps/mapDispatchToProps)
- robotFramework学习笔记
- Win10+VS2015环境下编译 OpenCV 3.1和opencv_contrib(网络整理)
- ReversibleString(81)