Autocompletetextview, how to search at any point in the sentence?

Asked

Viewed 78 times

1

Galley,

I have a Autocompletetextview and I need it to filter not just 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 brings only the KMF Avaré, but I need you to bring the Florisio KMF Sorocaba.

It is possible to search/filter from any point in the sentence?

  • It depends on how you are doing the filter. Show some code for how you have been doing.

1 answer

0


I managed to fix it. I did the following:

I created a Basedapter implementing Filterable and in it 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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.