我正在使用一个MultiAutoCompleteTextView
,并在其上设置自己的自定义适配器(BaseAdapter)。但是每次我在MultiAutoCompleteTextView中写东西时,performFiltering
方法中的约束始终为null值。
以下是我的BaseAdapter类的代码:
public class ListItemAdapter extends BaseAdapter implements Filterable{
ArrayList<ListItem> listItems;
ArrayList<ListItem> suggestions = new ArrayList<>();
private Filter filter = new CustomFilter();
Context context;
LayoutInflater inflater;
public ListItemAdapter(ArrayList<ListItem> listItems, Context context) {
this.listItems = listItems;
this.context = context;
inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return listItems.size();
}
@Override
public Object getItem(int position) {
return listItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
v = inflater.inflate(R.layout.single_item, null);
TextView title = (TextView) v.findViewById(R.id.title);
title.setText(listItems.get(position).getTitle());
return v;
}
@Override
public Filter getFilter() {
return filter;
}
private class CustomFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
suggestions.clear();
Log.d("FILTERRES", ""+listItems.size() + ", " + constraint);
if(listItems != null && constraint != null) {
for(int i=0 ; i<listItems.size(); i++) {
if(listItems.get(i).getTitle().toLowerCase().contains(constraint)) {
suggestions.add(listItems.get(i));
}
}
}
FilterResults results = new FilterResults();
results.values = suggestions;
results.count = suggestions.size();
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if(results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
}
以下是我如何将适配器设置为MultiAutoCompleteTextView:
MultiAutoCompleteTextView textView = (MultiAutoCompleteTextView) findViewById(R.id.auto);
ListItemAdapter adapter = new ListItemAdapter(items, this);
textView.setThreshold(1);
textView.setAdapter(adapter);
我在这里做错了什么?
编辑:
我将我的MultiAutoCompleteTextView
更改为AutoCompleteTextView
,现在它可以正常工作,有人知道如何使其适用于MultiAutoCompleteTextView吗?