我有一个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(); } }}
解决方法:
在适配器中实现Filterable,并添加此代码.
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; }}
总结 以上是内存溢出为你收集整理的android-AutoCompleteTextView隐藏和显示适配器更改下拉列表全部内容,希望文章能够帮你解决android-AutoCompleteTextView隐藏和显示适配器更改下拉列表所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)