The Android AutoCompleteTextView control USES an instance

  • 2020-06-01 10:57:12
  • OfStack

The main points of use are as follows:

1. Provide data for AutoCompleteTextView with ListAdapter (1), or overload getView() if necessary to customize the display of list items. This step is the same as ListView.
2. If you need to prompt dynamically according to the input content, adapter needs to implement Filterable interface and overload getFilter() function to realize the prompt algorithm. getFilter() returns an Filter object that contains at least two methods: performFiltering(), which performs filtering in the background, and publishResults(), which performs filtering in the UI thread, which displays the filtering results in a list. There is also a third method, convertResultToString(), that controls the text to be filled into the input box when the user clicks on the prompt.

The sample code is as follows:


AutoCompleteTextView autoComplete = new AutoCompleteTextView(context);
autoComplete.setThreshold(2);      //  Set the minimum number of characters to trigger autocomplete 
MyAdapter adapter = new MyAdapter(context);
autoComplete.setAdapter(adapter);
/**
 * Adapter define 
 */
class MyAdapter extends ArrayAdapter<MyObject> implements Filterable{
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // define your list item view here  A view of the list items is defined here 
    }
    /**
     *  Achieve automatic completion of the filtering algorithm 
     */
    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            /**
             *  This method is executed in the background thread to define the filtering algorithm 
             */
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                String keyword = String.valueOf(constraint).toLowerCase();
                //  Filtering is implemented here 
                //  Post-filtration utilization FilterResults Returns the filter result 
                FilterResults filterResults = new FilterResults();
                filterResults.values = results;   // results This is the filtering result above 
                filterResults.count = results.size();  //  Results the number of 
                return filterResults;
            }
            /**
             *  In this method UI Thread execution to update the autocomplete list 
             */
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                if (results != null && results.count > 0) {
                    //  There are filter results that show the autocomplete list 
                    MyAdapter.this.clear();   //  Empty the old list 
                    MyAdapter.this.addAll((List<MyObject>)results.values);
                    notifiDataSetChanged();
                } else {
                    //  Unfiltered results, close the list 
                    notifyDataSetInvalidated();
                }
            }
            /**
             *  Override this function if you need to control how the prompt text is displayed 
             */
            @Override
            public CharSequence convertResultToString(Object resultValue) {
                MyObject obj = (MyObject) resultValue;
                return obj.name;
            }
        };
    }
}


Related articles: