Skip to content

Instantly share code, notes, and snippets.

@chiemy
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chiemy/69f4616cd7aaa374d895 to your computer and use it in GitHub Desktop.
Save chiemy/69f4616cd7aaa374d895 to your computer and use it in GitHub Desktop.
/*
* This defines a one-element String array to contain the selection argument.
*/
String[] mSelectionArgs = {""};
// 从UI中获取单词
mSearchString = mSearchWord.getText().toString();
// Remember to insert code here to check for invalid or malicious input.
// 如果为空,则获取全部单词
if (TextUtils.isEmpty(mSearchString)) {
// 设置选择条件为空,会返回所有
mSelectionClause = null;
mSelectionArgs[0] = "";
} else {
// 构造可以匹配用户输入的单词的选择条件
mSelectionClause = UserDictionary.Words.WORD + " = ?";
// 将用户输入的单词作为选择参数
mSelectionArgs[0] = mSearchString;
}
// Does a query against the table and returns a Cursor object
mCursor = 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 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
} 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}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment