Search code examples
androidandroid-arrayadapterautocompletetextviewsimpleadapter

How to perform search based on more that one values in AutoCompleteTextView


I am using AutoCompleteTextView for searching City. I am using Array Adapter for binding AutoCompleteTextView. Now I want to enter either City or Zip Code on AutoCompleteTextView and want List of Cities based on that. How can I do it?


Solution

  • You can yse NameFilter in ArrayAdapter:

            @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();
                        }
                    }
                };