How to show suggestion list for Autocomplete Textview that shows only those words that begin with input character

1.3k Views Asked by At

I want to show the suggestion list that contains only those words that begins with input character not those word that contains the input character.

For example suppose I typed L than it will show me list like

["Launga Beach","Launga Hills","Launga Woods"] 

not like this

["Echo Lake,"Fields Landing","Green Vally Lake"]

here is my code

 ArrayAdapter<String> cityAdapter = new ArrayAdapter<String>(getActivity(),R.layout.auto_complete_select_dialog_item,mCityZipCodeMasterForCity.getCity());
        cityAutoCompleteTextView.setThreshold(1);
        cityAutoCompleteTextView.setAdapter(cityAdapter);
        cityAutoCompleteTextView.setOnFocusChangeListener(new AutoCompleteTextValidator());

Is there anyway to achieve this in autocomplete textview in android?

Thanks!

2

There are 2 best solutions below

0
On BEST ANSWER

Below I have added sample Adapter for your reference with Custom filter.

You just need your filtration logic.

CustomAdapter.java

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;

import java.util.ArrayList;
import java.util.List;


public class CustomAdapter extends ArrayAdapter<String> {

    Context context;
    int resource, textViewResourceId;
    List<String> items, tempItems, suggestions;

    public CustomAdapter(Context context, int resource, int textViewResourceId, List<String> items) {
        super(context, resource, textViewResourceId, items);
        this.context = context;
        this.resource = resource;
        this.textViewResourceId = textViewResourceId;
        this.items = items;
        tempItems = new ArrayList<String>(items); // this makes the difference.
        suggestions = new ArrayList<String>();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        // Handle your layout here
        return view;
    }

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

    /**
     * Custom Filter implementation for custom suggestions we provide.
     */
    Filter nameFilter = new Filter() {
        @Override
        public CharSequence convertResultToString(Object resultValue) {
            return resultValue.toString();
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            if (constraint != null) {
                suggestions.clear();

                // Here you can add your pattern like L* for starting string with 'L'
                for (String item : tempItems) {
                    if (item.startsWith("your pattern here")) {
                        suggestions.add(item);
                    }
                }
                FilterResults filterResults = new FilterResults();
                filterResults.values = suggestions;
                filterResults.count = suggestions.size();
                return filterResults;
            } else {
                return new FilterResults();
            }
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            List<String> filterList = (ArrayList<String>) results.values;
            if (results != null && results.count > 0) {
                clear();
                for (String item : filterList) {
                    add(item);
                    notifyDataSetChanged();
                }
            }
        }
    };
}

Thanks.

0
On

there is method of String

public boolean startsWith(String prefix)

example

String text = "searching my text";
boolean value = text.startsWith("searching"); // return true
boolean value1 = text.startsWith("my"); // return false

for ignoring the case

boolean value = text.toLowerCase().startsWith("searching".toLowerCase());