我有一个 AutoCompleteTextView,它通过 AsyncTask 从 API 获取建议。在 onPostExecute 我创建一个新适配器,将其设置为 AutoCompleteTextView 并将数据集更改通知给适配器。
在 TextWatcher 中,我执行 AsyncTask。
一切正常,除了每次更改适配器时都会关闭并显示下拉列表。
即使数据发生变化,如何保持下拉菜单打开?
searchText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
if (s.length() > 0) {
searchPlacesTask.cancel(true);
searchPlacesTask = new SearchPlacesTask();
searchPlacesTask.execute(s.toString().replace(" ", "-"));
} else {
searchPlacesTask.cancel(true);
searchPlacesTask = new SearchPlacesTask();
searchPlacesTask.execute();
}
}
});
private class SearchPlacesTask extends AsyncTask<String, Void, PlacesAPIResult> {
@Override
protected PlacesAPIResult doInBackground(String... params) {
PlacesAPIResult result = new PlacesAPIResult();
if (params.length > 0) {
places = PlacesAPIRestMethod.placeAutocomplete(params[0], currentLocation.getLatitude(), currentLocation.getLongitude(),
500, null, result);
} else {
places = PlacesAPIRestMethod.placeSearch(currentLocation.getLatitude(), currentLocation.getLongitude(), 0, true, result);
}
return result;
}
@Override
protected void onPostExecute(PlacesAPIResult result) {
if (result.getResult() == PlacesAPIResult.OK) {
adapter = new PlaceAdapter(ChooseLocationActivity.this, R.layout.locationrow, places);
searchText.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
}