我做了很多Google-fu,但我无法弄清楚出了什么问题。过滤工作,将出现下拉列表。但AutoCompleteTextView不会填充所选项目!有人可以帮忙吗?
我将自定义适配器设置为显示自定义布局的AutoCompleteTextView。
actv = (AutoCompleteTextView) root.findViewById(R.id.actv);
actv.setAdapter(new MyCustomAutoCompleteAdapter(getActivity()));
以下是重要部分MyCustomAutoCompleteAdapter
代码:
public class MyCustomAutoCompleteAdapter extends ArrayAdapter<String>
implements Filterable {
Context mContext;
private ArrayList<String> resultList = new ArrayList<>();
public MyCustomAutoCompleteAdapter(Context context) {
// is this the correct way to super?
super(context, R.layout.my_custom_layout);
mContext = context;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
...
return convertView;
}
@Override
public int getCount() {
return resultList.size();
}
@Override
public String getItem(int index) {
return resultList.get(index);
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint != null) {
// Retrieve the autocomplete results.
resultList = doPlacesSearchQuery(constraint.toString());
// Assign the data to the FilterResults
filterResults.values = resultList;
filterResults.count = resultList.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}
}};
return filter;
}
private ArrayList<String> doPlacesSearchQuery(String query) {
ArrayList<String> retList = new ArrayList<>();
... // do my API call here
return retList;
}
}
答案 0 :(得分:0)
我找到了答案;我确实使用了错误的超级构造函数。
public MyCustomAutoCompleteAdapter(Context context) {
super(context,
R.layout.my_custom_layout,
R.id.id_of_textview_in_my_custom_layout);
mContext = context;
}