Custom AutoCompleteTextView behavior

前端 未结 4 978
再見小時候
再見小時候 2020-12-19 02:45

Out of the box, the AutoCompleteTextView widget does not seem to be able to match the input string in the middle of a list value - the matches are always made a

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-19 03:10

    Old question, but still relevant. Following the guidance of a few other questions implemented a custom adapter using filterable. I made a simple generic adapter that searches with contains. Quick notes on it:

    I'm using butterknife, but easy to do the viewHolder with findviewbyid.

    The layout R.layout.list_item_simple is a simple layout with the textview R.id.text_view_simple.

    The object needs a toString that will be compared.

    public class SimpleContainsAutocompleteAdapter  extends ArrayAdapter implements Filterable {
        private List  listObjects;
        List suggestions = new ArrayList<>();
        private int resource;
    
        private Filter mFilter = new Filter(){
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults filterResults = new FilterResults();
    
                if(constraint != null) {
                    suggestions.clear();
                    for(T object : listObjects){
                        if(object.toString().toLowerCase().contains(constraint.toString().toLowerCase())){
                            suggestions.add(object);
                        }
                    }
    
                    filterResults.values = suggestions;
                    filterResults.count = suggestions.size();
                }
    
                return filterResults;
            }
    
            @Override
            protected void publishResults(CharSequence contraint, FilterResults results) {
                if(results == null){
                    return;
                }
    
                List filteredList = (List) results.values;
                if(results.count > 0) {
                    clear();
                    for (T filteredObject : filteredList) {
                        add(filteredObject);
                    }
                    notifyDataSetChanged();
                }
            }
        };
    
        public SimpleContainsAutocompleteAdapter(Context context, List listObjects) {
            super(context, R.layout.list_item_simple, listObjects);
            this.listObjects = new ArrayList<>(listObjects);
            this.resource = R.layout.list_item_simple;
        }
    
        @Override
        public Filter getFilter() {
            return mFilter;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Object listObject = getItem(position);
            viewHolder holder;
            if(convertView != null) {
                holder = (viewHolder) convertView.getTag();
            }else{
                convertView = LayoutInflater.from(getContext()).inflate(resource, parent, false);
                holder = new viewHolder(convertView);
                convertView.setTag(holder);
            }
    
            holder.name.setText(listObject.toString());
    
            return convertView;
        }
    
    
        static class viewHolder {
            @InjectView(R.id.text_view_simple) TextView name;
    
            public viewHolder(View view) {
                ButterKnife.inject(this, view);
            }
        }
    }
    

提交回复
热议问题