AutoCompleteTextView, how to do search at any point in the sentence?

1

Galera,

I have an AutoCompleteTextView and I need it to filter not just by the beginning of the sentence but any part of it.

ex > My list: KMF Avaré, Florisio (KMF Sorocaba)

When I type the letter "K" it just brings the KMF Avaré , but I need you to bring the Florisio KMF Sorocaba as well.

Is it possible to do the search / filter from any point in the sentence?

    
asked by anonymous 16.07.2014 / 15:45

1 answer

0

I managed to resolve. I've done the following:

I created a BaseAdapter implementing Filterable and I made the filter logic:

@Override
public Filter getFilter() {

    Filter filter = new Filter() {          
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {

            if (results != null && results.count > 0) {
                notifyDataSetChanged();
            }
            else {
                notifyDataSetInvalidated();
            }
        }

        @SuppressLint("DefaultLocale") @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            listaAdapter = new ArrayList<String>();

            if (constraint != null) {

                for (String item : listaCompleta) {                     
                    if (item.toLowerCase().contains(constraint.toString().toLowerCase())) {
                        listaAdapter.add(item);
                    }
                }
            }
            else listaAdapter = listaCompleta;

            FilterResults results = new FilterResults();
            results.values = listaAdapter;
            results.count = listaAdapter.size();

            return results;
        }
    };

    return filter;
}
    
16.07.2014 / 20:47