简体   繁体   English

AutoCompleteTextView - 禁用过滤

[英]AutoCompleteTextView - disable filtering

I'm retrieving a list of strings from a webservice and I want to list them up on a AutoCompleteTextField regardless of the built-in AutoCompleteTextField filters.我正在从webservice服务中检索字符串列表,并且我想将它们列在AutoCompleteTextField上,而不管内置的AutoCompleteTextField过滤器。

How do I do that?我怎么做? is there a way to disable it's inner filtering easily (preferably without subclassing) I've loaded all my results into a ArrayAdapter , the problem is that some of them don't show up because of the filtering.有没有办法轻松禁用它的内部过滤(最好没有子类化)我已经将所有结果加载到ArrayAdapter中,问题是其中一些由于过滤而没有显示。

If I'm going in the wrong direction please point me to the right direction.如果我走错了方向,请指出正确的方向。

Probably @Alon meant subclassing ArrayAdapter , instead of AutoCompleteTextView .可能 @Alon 意味着子类化ArrayAdapter ,而不是AutoCompleteTextView In getFilter() method one has to return a custom filter, that performs no filtering at all (in its performFiltering() ).getFilter()方法中,必须返回一个自定义过滤器,它根本不执行任何过滤(在它的performFiltering() )。 Probably the performance could be a problem - because theread is spawned.性能可能是一个问题 - 因为产生了读取。 The best thing would be to derive from TextEdit and implement own completion popup.最好的办法是从 TextEdit 派生并实现自己的完成弹出窗口。 But this is again too many hassle for me, so far.但到目前为止,这对我来说又太麻烦了。 Finally, I did something as follows and it works for me.最后,我做了以下事情,它对我有用。 Any feedback appreciated.任何反馈表示赞赏。

public class KArrayAdapter<T> 
extends ArrayAdapter<T>
{
    private Filter filter = new KNoFilter();
    public List<T> items;

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

    public KArrayAdapter(Context context, int textViewResourceId,
            List<T> objects) {
        super(context, textViewResourceId, objects);
        Log.v("Krzys", "Adapter created " + filter);
        items = objects;
    }

    private class KNoFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence arg0) {
            FilterResults result = new FilterResults();
                result.values = items;
                result.count = items.size(); 
            return result;
        }

        @Override
        protected void publishResults(CharSequence arg0, FilterResults arg1) {
            notifyDataSetChanged();
        }
    }
}

Hope it helps.希望能帮助到你。

When setting text to the AutoCompleteTextView, use setText(CharSequence text, boolean filter) .将文本设置为 AutoCompleteTextView 时,请使用setText(CharSequence text, boolean filter) Set filter to false, this sets the text without activating the filter将过滤器设置为 false,这会在不激活过滤器的情况下设置文本

I solved my problem by making a custom adapter extending ArrayAdapter class and overriding its getFilter() method.我通过制作一个扩展ArrayAdapter类的自定义适配器并覆盖其getFilter()方法来解决我的问题。 By doing this the list will not be filtered based on any text placed in the TextField and all items will be displayed.通过这样做,列表将不会根据放置在 TextField 中的任何文本进行过滤,并且将显示所有项目。

public class MyAdapter extends ArrayAdapter{
    public MyAdapter(@NonNull Context context, int resource) {
        super(context, resource);
    }

    public MyAdapter(@NonNull Context context, int resource, int textViewResourceId) {
        super(context, resource, textViewResourceId);
    }

    public MyAdapter(@NonNull Context context, int resource, @NonNull Object[] objects) {
        super(context, resource, objects);
    }

    public MyAdapter(@NonNull Context context, int resource, int textViewResourceId, @NonNull Object[] objects) {
        super(context, resource, textViewResourceId, objects);
    }

    public MyAdapter(@NonNull Context context, int resource, @NonNull List objects) {
        super(context, resource, objects);
    }

    public MyAdapter(@NonNull Context context, int resource, int textViewResourceId, @NonNull List objects) {
        super(context, resource, textViewResourceId, objects);
    }

    @NonNull
    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                return null;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {

            }
        };
    }
}

最终我将ArrayAdapter子类化,通过覆盖它的getFilter方法禁用过滤器,并在“文本更改”事件期间创建我的HTTPRequest

Found this while looking for a similar thing, but in what could be a bit unusual setup - using AutocompleteTextView as a Spinner (it's useful when you want a spinner wrapped in TextInputLayout ).在寻找类似的东西时发现了这个,但是在可能有点不寻常的设置中 - 使用AutocompleteTextView作为微调器(当你想要一个包裹在TextInputLayout中的微调器时它很有用)。 I'm also using app:simpleItems approach for providing items instead of a custom adapter like so:我还使用app:simpleItems方法来提供项目而不是像这样的自定义适配器:

<com.google.android.material.textfield.MaterialAutoCompleteTextView
                                android:id="@+id/sizes"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:inputType="none"
                                app:simpleItems="@array/sizes" />

There are two approaches that worked and don't require you to subclass the adapter:有两种方法可行并且不需要您对适配器进行子类化:

  1. Call setText(text, false) in your onItemClickListeneronItemClickListener中调用setText(text, false)
view.findViewById<MaterialAutoCompleteTextView>(R.id.sizes).onItemClickListener =
        AdapterView.OnItemClickListener { _, _, position, _ ->
            setText(adapter.getItem(position).toString(), false);
        }
  1. Set empty array to filters.将空数组设置为过滤器。
view.findViewById<MaterialAutoCompleteTextView>(R.id.sampler).filters = arrayOf()

Hope this helps someone.希望这对某人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM