android - 将 AutoCompleteTextView 过滤器从 "startsWith"更改为 "Contains"?

标签 android autocompletetextview

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

这可能吗?

最佳答案

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

autocomplete_item.xml

<?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>

为你的名字创建新类:

名字

public class Names {
    public String name;
}

名称适配器

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(或您的主要 Activity )

....
   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();
...

关于android - 将 AutoCompleteTextView 过滤器从 "startsWith"更改为 "Contains"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35063624/

相关文章:

android - 如何在Android Compose中使用otf类型的字体文件?

android - 自定义 Android AutoCompleteTextView

java - 当 AutoCompleteTextView 获得焦点时隐藏元素

android - 如何使用类中创建的字符串数组填充 autocompletetextview

java - 在自动完成 TextView 中获取后退空格按钮单击操作事件

android - 禁止导入 Crashlytics

android - 从json解析纬度和经度后如何在谷歌地图中放置标记?

Android 模拟器错误消息 : "PANIC: Missing emulator engine program for ' x8 6' CPUS."

android - Facebook Audience Network - (#606) 申请被拒绝

android - 从联系人列表中获取姓名和电子邮件非常慢