【问题标题】:How to perform search based on more that one values in AutoCompleteTextView如何根据 AutoCompleteTextView 中的多个值执行搜索
【发布时间】:2014-06-02 08:11:22
【问题描述】:

我正在使用 AutoCompleteTextView 来搜索城市。我正在使用数组适配器来绑定 AutoCompleteTextView。现在我想在 AutoCompleteTextView 上输入城市或邮政编码,并希望以此为基础列出城市。我该怎么做?

【问题讨论】:

  • AutocompleTextView 实现自定义适配器,从getFilter() 方法返回自定义Filter。在 FilterperformFiltering() 方法中,查看传入的约束并检查它是字符串(很可能是城市名称)还是数字(我假设是数字)。基于该知识过滤结果。此外,扩展BaseAdapter 更适合于此。

标签: android android-arrayadapter autocompletetextview simpleadapter


【解决方案1】:

您可以在 ArrayAdapter 中使用 NameFilter:

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

        Filter cityFilter = new Filter() {
            public String convertResultToString(Object resultValue) {
                String str = ((YourObject) (resultValue)).getName();
                return str;
            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
            //Your code for find CIty by name or zip 
        }

           @Override
                protected void publishResults(CharSequence constraint,
                        FilterResults results) {
                //You can now update youArrayList and after you need call notifyDataSetChanged
                        notifyDataSetChanged();
                    }
                }
            };

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    • 2018-03-11
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    • 2017-06-10
    相关资源
    最近更新 更多