i am facing a problem in using content resolver to get data from UserDictionary.words content provider.
Here is my requirements: 1. I want to find out the words of UserDictionary that matches with the given word.
My implementation; 1. my app has two screen (Activities) 2. i enter a word ont he first screen ans pass this as a intent to second screen. on the second screen i want to display the list of words of Userdictionary that matches with the given word on the first screen.
My problem is... i was able to pass the word to second activity but missing out to retrieve the records of userDicitnary. what ever the word i give it is returning records. Here is my code of the two activities. Please help me in resolving the issue.
First activity:
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);
}
second activity:
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);
}
}
In TextUtils.isEmpty() {}
clause I would suggest to change:
mSelectionArgs[0] = "";
to
mSelectionArgs = null;
because of the possible illegal argument exception:
java.lang.IllegalArgumentException:
Cannot bind argument at index 1 because the index is out of range.
The statement has 0 parameters.