I am doing a dictionary app based on this project. This is my AutoCompleteTextView:
searchAutoCompleteTextView.setThreshold(1);
searchAutoCompleteTextView.setAdapter(new WordAutoComplite(ZhuangDictActivity.this, null));
searchAutoCompleteTextView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (s.length() > 0) {
char key = GB2Alpha.Char2Alpha(s.charAt(0));
if (currentChar != key) {
currentChar = key;
tableName = DatabaseHelper.transTableName(key);
}
}
}
});
My CursorAdapter:
@Override
public Cursor runQueryOnBackgroundThread(CharSequence word) {
return databaseHelper.queryAutoComplete(tableName, word.toString(), DEFAULT_AUTOCOMPLETE_LIMIT);
}
/* (non-Javadoc)
* @see android.widget.CursorAdapter#convertToString(android.database.Cursor)
*/
@Override
public CharSequence convertToString(Cursor cursor) {
return cursor.getString(0);
}
The DatabaseHelper.java is available here and here is the complete code of my activity.
I noticed a bug. Let say I search "apple" and I exit the app. Then I reenter the app and search "apple" again, this time the result cannot be found. In order to solve this problem, I have to type another alphabet randomly other than "a". If after I exit the app and return to it, I search "book" before searching "apple", I will be able to get the result. Seem like the app lost connection to the database. How should I solve this problem?