我有一个使用 ArrayAdapter 的 AutocompleteTextView。当文本更改时,适配器会从 Web 服务更新。此更新是在 AsyncTask 中完成的,这应该是一个很好的做法。这或多或少是有效的,因为每次按下按键后的建议都是基于在上一次按下的按键中检索到的字符串。这个页面有几个相关的问题,但没有一个答案对我有用。无论如何,我有一个解决方案,但效率低下,我不知道为什么“官方解决方案”会失败。
我认为关键在于在后台更新 de ArrayAdapter 的功能。这就是我在对 web 服务的异步调用中所做的:
private class DoAutoCompleteSearch extends AsyncTask<String, Void, Map<String, String>> {
@Override
protected Map<String, String> doInBackground(String... params) {
// Ask the webservice for data
Map<String, String> autoComplete = GetResource.dataList(params[0]);
return autoComplete;
}
@Override
protected void onPostExecute(Map<String, String> result) {
//mAutoCompleteAdapter.clear(); * This should work but does not *
/* If it is set a new adapter in the AutoCompleteTextView, the whole thing works properly */
mAutoCompleteAdapter = new ArrayAdapter<String>(mAutoCompleteAdapter.getContext(), android.R.layout.simple_dropdown_item_1line);
mACTV.setAdapter(mAutoCompleteAdapter);
for (Map.Entry<String, String> entry : result.entrySet()) {
mAutoCompleteAdapter.add(entry.getKey());
}
}
}
我已经尝试使用 mAutoCompleteAdapter.clear() 并在任何地方设置 mAutoCompleteAdapter.notifyDataSetChanged() ,但它没用。