好吧,我试图在每次使用 TextWathcer 更改文本时动态更新 autoCompleteTextview ArrayAdapter。
每次文本更改时,在新的 AsyncTask 和来自异步任务(onPostExecute)的回调中都会有一个 http 请求,我从适配器调用 clear() 并向他添加新项目,然后调用 notifyDataSetChanged。不幸的是,自动完成下拉菜单从未显示..
请问,我要去哪里穿?!
这是代码:
AutoCompleteTextView acCountry = (AutoCompleteTextView)layout.findViewById(R.id.autoComplete);
final ArrayAdapter<RMAutoComplete.ListItem> countriesAdapter = new ArrayAdapter<RMAutoComplete.ListItem>(this.context,android.R.layout.simple_dropdown_item_1line);
acCountry.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() > 1)
{
new AsyncTask<String, Void, List<RMAutoComplete.ListItem>>(){
@Override
protected List<ListItem> doInBackground(String... params) {
List<ListItem> l = null;
try {
l = location.getCountryData(params[0]);
} catch (Exception e) {
Log.e(TAG,"error when getCountryData",e);
}
return l;
}
@Override
protected void onPostExecute(List<ListItem> countries) {
countriesAdapter.clear();
if (countries != null)
for (ListItem listItem : countries) {
countriesAdapter.add(listItem);
}
countriesAdapter.notifyDataSetChanged();
}
}.execute(s.toString());
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void afterTextChanged(Editable s) {}
}
);