AutoCompleteTextView not completing words inside parentheses

后端 未结 6 396
灰色年华
灰色年华 2020-12-03 00:11

I have implemented AutoCompleteTextView as follows:

MainActivity.java

...
    public static String[] myData=new String[         


        
6条回答
  •  一向
    一向 (楼主)
    2020-12-03 00:51

    An answer, provided by @BNK, is correct. However, I would like to give a similar solution, which doesn't require the whole ArrayAdapter class file. Instead, we will just extend that class and override its only 2 methods: getView() and getFilter(). So, define your AutoSuggestAdapter class:

    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.Filter;
    import android.widget.TextView;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class AutoSuggestAdapter extends ArrayAdapter
    {
        private Context      context;
        private int          resource;
        private List items;
        private List tempItems;
        private List suggestions;
    
        public AutoSuggestAdapter(Context context, int resource, List items)
        {
            super(context, resource, 0, items);
    
            this.context = context;
            this.resource = resource;
            this.items = items;
            tempItems = new ArrayList(items);
            suggestions = new ArrayList();
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            View view = convertView;
            if (convertView == null)
            {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(resource, parent, false);
            }
    
            String item = items.get(position);
    
            if (item != null && view instanceof TextView)
            {
                ((TextView) view).setText(item);
            }
    
            return view;
        }
    
        @Override
        public Filter getFilter()
        {
            return nameFilter;
        }
    
        Filter nameFilter = new Filter()
        {
            @Override
            public CharSequence convertResultToString(Object resultValue)
            {
                String str = (String) resultValue;
                return str;
            }
    
            @Override
            protected FilterResults performFiltering(CharSequence constraint)
            {
                if (constraint != null)
                {
                    suggestions.clear();
                    for (String names : tempItems)
                    {
                        if (names.toLowerCase().contains(constraint.toString().toLowerCase()))
                        {
                            suggestions.add(names);
                        }
                    }
                    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 filterList = (ArrayList) results.values;
                if (results != null && results.count > 0)
                {
                    clear();
                    for (String item : filterList)
                    {
                        add(item);
                        notifyDataSetChanged();
                    }
                }
            }
        };
    }
    

    Define auto complete view in XML for example:

           
    

    And use it:

        AutoCompleteTextView autoComplete = (AutoCompleteTextView) findViewById(R.id.autoComplete);
    
        List  stringList = new ArrayList();
        stringList.add("Black");
        stringList.add("White");
        stringList.add("Yellow");
        stringList.add("Green");
        stringList.add("Blue");
        stringList.add("Brown");
        stringList.add("Orange");
        stringList.add("Pink");
        stringList.add("Violet");
        stringList.add("Cyan");
        stringList.add("LightBlue");
    
        AutoSuggestAdapter adapter = new AutoSuggestAdapter(this, android.R.layout.simple_list_item_1, stringList);
    
        autoComplete.setAdapter(adapter);
    
        // specify the minimum type of characters before drop-down list is shown
        autoComplete.setThreshold(1);
    

提交回复
热议问题