接入用户词典机器人
问题描述:
这是我的code,以获得用户词典的话。这是我的code,但我不能够运行它...
Hi this is my code to get the words from the user dictionary. this is my code but i am not able to run it...
private String getwordlist() {
String[] mSelectionArgs={""};
String[] mProjection ={UserDictionary.Words._ID, UserDictionary.Words.WORD, UserDictionary.Words.FREQUENCY};
String mSelectionClause = null;
String mSortOrder = null;
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
if (mCursor.moveToFirst()) {
do {
String id = mCursor.getString(mCursor.getColumnIndex(UserDictionary.Words._ID));
String word = mCursor.getString(mCursor.getColumnIndex(UserDictionary.Words.WORD));
String freq= mCursor.getString(mCursor.getColumnIndex(UserDictionary.Words.FREQUENCY));
str=str+" id: "+id+" word: "+" frequency: "+freq+"\n";
System.out.println(str);
} while(mCursor.moveToNext());
return str;
}
return null;
}
我得到误差
12-06 18:49:58.586: E/AndroidRuntime(17174): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.ScrollView}: java.lang.IllegalArgumentException: Cannot bind argument at index 1 because the index is out of range. The statement has 0 parameters.
什么是错误的..
what is the error in this..
答
好像当mSelectionClause为空,mSelectionArgs需要为空了。
it seems like that when mSelectionClause is null, mSelectionArgs need to be null too.
// If the word is the empty string, gets everything
if (TextUtils.isEmpty(searchWord)) {
// Setting the selection clause to null will return all words
mSelectionClause = null;
mSelectionArgs = null;
} else {
// Constructs a selection clause that matches the word that the user entered
mSelectionClause = UserDictionary.Words.WORD + " = ?";
// Moves the user's input to the selection arguments
mSelectionArgs = new String[] {searchWord};
}
// Does a query against the table and returns a Cursor object
Cursor cursor = 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
null);