コンテンツリゾルバーを使用してUserDictionary.wordsコンテンツプロバイダーからデータを取得する際に問題が発生しています。
要件は次のとおりです。1。指定された単語と一致するUserDictionaryの単語を見つけたい。
私の実装; 1.私のアプリには2つの画面(アクティビティ)があります。2。最初の画面に単語を入力し、これを2番目の画面へのインテントとして渡します。2番目の画面で、最初の画面で指定された単語と一致するUserdictionaryの単語のリストを表示したいと思います。
私の問題は...単語を2番目のアクティビティに渡すことができましたが、userDicitnaryのレコードを取得できませんでした。私がそれを与える言葉が何であれ、レコードを返すことです。これが2つのアクティビティの私のコードです。問題の解決にご協力ください。
最初の活動:
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);
}
2番目のアクティビティ:
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);
}
}