适配器上的变化AutoCompleteTextView隐藏和显示下拉 [英] AutoCompleteTextView hide and show dropdown on adapter change
问题描述
我从与AsyncTask的一个API获取建议的AutoCompleteTextView。
在onPostExecute我创建一个新的适配器,将其设置为AutoCompleteTextView并通知数据集更改适配器。
在TextWatcher我执行的AsyncTask。
一切正常,只是下拉被驳回,每个适配器改变时表示。
我如何能保持下拉开放甚至在数据发生了变化?
searchText.addTextChangedListener(新TextWatcher(){
@覆盖
公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
// TODO自动生成方法存根 } @覆盖
公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
INT后){
// TODO自动生成方法存根 } @覆盖
公共无效afterTextChanged(编辑S){
如果(s.length()大于0){
searchPlacesTask.cancel(真);
searchPlacesTask =新SearchPlacesTask();
searchPlacesTask.execute(s.toString()取代(, - )。);
}其他{
searchPlacesTask.cancel(真);
searchPlacesTask =新SearchPlacesTask();
searchPlacesTask.execute();
}
}
});私有类SearchPlacesTask扩展的AsyncTask<弦乐,太虚,PlacesAPIResult> { @覆盖
保护PlacesAPIResult doInBackground(字符串... PARAMS){
PlacesAPIResult结果=新PlacesAPIResult(); 如果(params.length大于0){
地方= PlacesAPIRestMethod.placeAutocomplete(PARAMS [0],currentLocation.getLatitude(),currentLocation.getLongitude(),
500,空,结果);
}其他{
地方= PlacesAPIRestMethod.placeSearch(currentLocation.getLatitude(),currentLocation.getLongitude(),0,真实,结果);
} 返回结果;
} @覆盖
保护无效onPostExecute(PlacesAPIResult结果){
如果(result.getResult()== PlacesAPIResult.OK){
适配器=新PlaceAdapter(ChooseLocationActivity.this,R.layout.locationrow,地点);
searchText.setAdapter(适配器);
adapter.notifyDataSetChanged();
}
}
}
实现过滤的适配器,并添加此code。
公共类ListAdapter扩展ArrayAdapter<串GT;实现可筛选{
私人列表<串GT; listResult; ...
@覆盖
公共过滤用getFilter(){
过滤器过滤器=新的过滤器(){ @覆盖
保护FilterResults performFiltering(CharSequence的约束){
FilterResults filterResults =新FilterResults();
如果(约束!= NULL){
//分配数据到FilterResults
filterResults.values = listResult;
filterResults.count = listResult.size();
}
返回filterResults;
} @覆盖
保护无效publishResults(CharSequence的约束,FilterResults结果){
如果(结果= NULL&放大器;!&安培; results.count大于0){
notifyDataSetChanged();
}
其他{
notifyDataSetInvalidated();
}
}};
返回过滤器;
}
}
I have an AutoCompleteTextView that gets suggestion from an API with an AsyncTask. in onPostExecute i create a new adapter, set it to the AutoCompleteTextView and notify data set change to the adapter.
in a TextWatcher I execute the AsyncTask.
everything is working fine except that the dropdown is dismissed and shown every time the adapter is changed.
how can I keep the dropdown open even while the data is changing ?
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();
}
}
}
Implements Filterable in adapter, and add this code.
public class ListAdapter extends ArrayAdapter<String> implements Filterable{
private List<String> listResult;
...
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint != null) {
// Assign the data to the FilterResults
filterResults.values = listResult;
filterResults.count = listResult.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}
}};
return filter;
}
}
这篇关于适配器上的变化AutoCompleteTextView隐藏和显示下拉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!