【发布时间】:2012-02-02 12:42:53
【问题描述】:
我正在动态更新 AutoCompleteTextView,我面临两个问题。
当项目被选中时,会有一个新的 onTextChanged 事件,正如您在下面的代码中看到的,有一个获取新可选项目的新请求,因此 onTextChanged 事件会导致自动完成再次显示下拉菜单!有没有干净的方法来解决它?!
在调用 notifyDataSetChange() 之前,我从自动完成获得的结果来自上一个适配器,我该如何实现?!
代码如下:
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.setAdapter(countriesAdapter);
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) {}
}
);
【问题讨论】:
标签: java android autocomplete