我在使用内容解析器从UserDictionary.words内容提供商处获取数据时遇到问题。
这是我的要求: 1.我想找出UserDictionary与给定单词匹配的单词。
我的实施; 1.我的应用程序有两个屏幕(活动) 2.我在第一个屏幕上输入一个单词并将其作为第二个屏幕的意图传递。 在第二个屏幕上,我想在第一个屏幕上显示与给定单词匹配的Userdictionary单词列表。
我的问题是......我能够将这个词传递给第二个活动,但错过了检索userDicitnary的记录。我给它的这个词是什么回归记录。这是我的两个活动的代码。请帮我解决这个问题。
第一项活动:
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG,"onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content_user_demo);
}
public void SendWord(View view){
Log.d(TAG,"SendWord");
EditText SearchWord = (EditText) findViewById(R.id.mSearchWord);
String mSearchString = SearchWord.getText().toString();
Intent intent = new Intent(this, WordListActivity.class);
Log.d(TAG,"EXTRA_MESSAGE");
intent.putExtra(EXTRA_MESSAGE, mSearchString);
startActivity(intent);
}
第二项活动:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG,"onCreate");
setContentView(R.layout.activity_word_list);
Intent intent = getIntent();
Log.d(TAG,"getIntent() EXTRA_MESSAGE");
String searchWord = intent.getStringExtra(ContentUserDemo.EXTRA_MESSAGE);
textView = new TextView(this);
Log.d(TAG,"searchWord " +searchWord);
textView.setTextSize(20);
setContentView(textView);
Log.d(TAG,"textSize " +textView.length());
String[] mProjection =
{
UserDictionary.Words._ID,
UserDictionary.Words.WORD,
UserDictionary.Words.LOCALE
};
String mSelectionClause = null;
String[] mSelectionArgs = {""};
String mSortOrder=null;
ContentResolver cr = getContentResolver();
if (TextUtils.isEmpty(searchWord)) {
mSelectionClause = null;
mSelectionArgs[0] = "";
}
else {
mSelectionClause = UserDictionary.Words.WORD + " = ?";
mSelectionArgs[0] = searchWord;
}
Cursor mCursor = cr.query(UserDictionary.Words.CONTENT_URI,
mProjection,
mSelectionClause,
mSelectionArgs,
mSortOrder);
startManagingCursor(mCursor);
int count = mCursor.getCount();
if (null == mCursor) {
Log.d(TAG, "cursor.getCount()=" + count);
String message="No word matches with "+ searchWord;
textView.setText(message);
}
else if (mCursor.getCount() < 1) {
Log.d(TAG, "cursor.getCount()=" + count);
String message="getCount is less than 1 " + "No word matches with "+ searchWord;
textView.setText(message);
}
else {
String message="Else part of the code";
textView.setText(message);
String[] mWordListColumns ={UserDictionary.Words.WORD,UserDictionary.Words.LOCALE};
int[] mWordListItems = { R.id.dictWord, R.id.locale};
SimpleCursorAdapter mCursorAdapter = new SimpleCursorAdapter(
getApplicationContext(),
R.layout.wordlistrow,
mCursor,
mWordListColumns,
mWordListItems,
0);
ListView mWordList = (ListView) findViewById(R.id.mWordList);
mWordList.setAdapter(mCursorAdapter);
}
}
答案 0 :(得分:1)
在TextUtils.isEmpty() {}
条款中我建议改变:
mSelectionArgs[0] = "";
来
mSelectionArgs = null;
因为可能存在非法参数异常:
java.lang.IllegalArgumentException:
Cannot bind argument at index 1 because the index is out of range.
The statement has 0 parameters.