从"startsWith"更改AutoCompleteTextView过滤器“包含"? [英] Change AutoCompleteTextView filter from "startsWith" to "Contains"?

查看:69
本文介绍了从"startsWith"更改AutoCompleteTextView过滤器“包含"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改AutoCompleteTextView中的默认过滤.默认过滤将查找以给定标记开始的所有字符串.我的项目要求过滤应找到包含给定标记的所有字符串.

I would like to change the default filtering in AutoCompleteTextView. The default filtering finds all strings that startsWith the given token. My project requires that the filtering should find all strings that contains the given token.

有可能吗?

推荐答案

感谢Google并进行了为期两天的搜索,我找到了一个解决方案.正如@ torque203所建议的,我已经实现了自己的自定义适配器.首先在适配器中为自定义Item定义一个新的XML文件:

I found a solution for that, thanks to Google and searching for two days. As @torque203 suggested, I've implemented my own custom Adapter. First define a new XML file to custom Item in the adapter:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:paddingTop="16dp"
        android:paddingBottom="16dp"
        android:id="@+id/lbl_name" />
</RelativeLayout>


为您的姓名创建新类:


Create new class for your Names:

public class Names {
    public String name;
}


NamesAdapter

public class NamesAdapter extends ArrayAdapter<Names> {

    Context context;
    int resource, textViewResourceId;
    List<Names> items, tempItems, suggestions;

    public NamesAdapter(Context context, int resource, int textViewResourceId, List<Names> items) {
        super(context, resource, textViewResourceId, items);
        this.context = context;
        this.resource = resource;
        this.textViewResourceId = textViewResourceId;
        this.items = items;
        tempItems = new ArrayList<Names>(items); // this makes the difference.
        suggestions = new ArrayList<Names>();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.autocomplete_item, parent, false);
        }
        Names names = items.get(position);
        if (names != null) {
            TextView lblName = (TextView) view.findViewById(R.id.lbl_name);
            if (lblName != null)
                lblName.setText(names.name);
        }
        return view;
    }

    @Override
    public Filter getFilter() {
        return nameFilter;
    }

    /**
     * Custom Filter implementation for custom suggestions we provide.
     */
    Filter nameFilter = new Filter() {
        @Override
        public CharSequence convertResultToString(Object resultValue) {
            String str = ((Names) resultValue).name;
            return str;
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            if (constraint != null) {
                suggestions.clear();
                for (Names names : tempItems) {
                    if (names.name.toLowerCase().contains(constraint.toString().toLowerCase())) {
                        suggestions.add(names);
                    }
                }
                FilterResults filterResults = new FilterResults();
                filterResults.values = suggestions;
                filterResults.count = suggestions.size();
                return filterResults;
            } else {
                return new FilterResults();
            }
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            List<Names> filterList = (ArrayList<Names>) results.values;
            if (results != null && results.count > 0) {
                clear();
                for (Names names : filterList) {
                    add(names);
                    notifyDataSetChanged();
                }
            }
        }
    };
}


SearchActivity(或您的主要活动)

....
   List<Names> namesList =  //your names list;
   NamesAdapter namesAdapter = new NamesAdapter(
                    SearchActivity.this,
                    R.layout.activity_search,
                    R.id.lbl_name,
                    namesList
            );
            //set adapter into listStudent
            autoCompleteTextView.setAdapter(namesAdapter);
            autoCompleteTextView.showDropDown();
...

这篇关于从"startsWith"更改AutoCompleteTextView过滤器“包含"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆