其他分享
首页 > 其他分享> > 带有自定义适配器(BaseAdapter)的MultiAutoCompleteTextView

带有自定义适配器(BaseAdapter)的MultiAutoCompleteTextView

作者:互联网

我正在使用MultiAutoCompleteTextView并在其上设置我自己的自定义适配器(BaseAdapter).但是,每次我在MultiAutoCompleteTextView中编写内容时,performFiltering方法中的约束始终为我编写的每个字符获取空值.

这是我的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吗?

解决方法:

您需要将setTokenizer()设置为MultiAutoCompleteTextView才能起作用.

您可以使用 –

 textView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

如果您有任何特殊要求,即使您可以设置自己的自定义令牌生成器,也可以使用CommaTokenizer,每次从下拉列表中选择任何选项时,它都会附加comma(,).

标签:autocompletetextview,filter,android
来源: https://codeday.me/bug/20191118/2031547.html