Android 连接到数据库的AutoCompleteTextView
我正在做一个基于的字典应用程序。这是我的AutoCompleteTextView:Android 连接到数据库的AutoCompleteTextView,android,Android,我正在做一个基于的字典应用程序。这是我的AutoCompleteTextView: searchAutoCompleteTextView.setThreshold(1); searchAutoCompleteTextView.setAdapter(new WordAutoComplite(ZhuangDictActivity.this, null)); searchAutoCompleteTextView.addTextChangedListener(new Te
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);
}
}
}
});
我的光标适配器:
@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);
}
下面是我活动的重点
我注意到一只虫子。假设我搜索“苹果”,然后退出应用程序。然后我重新进入应用程序并再次搜索“apple”,这次找不到结果。为了解决这个问题,我必须随机键入除“a”之外的另一个字母表。如果在我退出并返回应用程序后,在搜索“苹果”之前搜索“书籍”,我将能够得到结果。应用程序似乎与数据库失去了连接。我该如何解决这个问题?请看我50分钟前在这里写的ny评论,你是说我应该在postexecute上清除适配器?好的,现在我看到你在幕后使用了一个过滤器,那么TextWatcher有什么用呢?要在我们键入时显示下拉式自动完成搜索建议。你不需要它,请看我的答案,在您的情况下,您不需要FilterQueryProvider,您可以在runQueryOnBackgroundThread中执行相同的操作