AutoCompleteTextView全局匹配

       AutoCompleteTextView 这个东西做Android的应该都看过~没看过的去百度下吧,我就不多说了,问题是它怎么能自动补全的呢?

      这个是因为它setAdapter(adapter);,没错,你没看错,就是因为它设置了一个adapter。。。而它的过滤原则也是根据这个adapter中的Filter来的。

      一般网上的那些例子里面都是设置的一个ArrayAdapter,这个adapter里面的Filter看源码就能看出来是怎么过滤的:

 public Filter getFilter() {
        if (mFilter == null) {
            mFilter = new ArrayFilter();
        }
        return mFilter;
    }

    /**
     * <p>An array filter constrains the content of the array adapter with
     * a prefix. Each item that does not start with the supplied prefix
     * is removed from the list.</p>
     */
    private class ArrayFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence prefix) {
            FilterResults results = new FilterResults();

            if (mOriginalValues == null) {
                synchronized (mLock) {
                    mOriginalValues = new ArrayList<T>(mObjects);
                }
            }

            if (prefix == null || prefix.length() == 0) {
                ArrayList<T> list;
                synchronized (mLock) {
                    list = new ArrayList<T>(mOriginalValues);
                }
                results.values = list;
                results.count = list.size();
            } else {
                String prefixString = prefix.toString().toLowerCase();

                ArrayList<T> values;
                synchronized (mLock) {
                    values = new ArrayList<T>(mOriginalValues);
                }

                final int count = values.size();
                final ArrayList<T> newValues = new ArrayList<T>();

                for (int i = 0; i < count; i++) {
                    final T value = values.get(i);
                    final String valueText = value.toString().toLowerCase();

                    // First match against the whole, non-splitted value
                    if (valueText.startsWith(prefixString)) {//看到没有~这就是匹配原则。。。就是这么蛋疼,就是这么酸爽
                        newValues.add(value);
                    } else {
                        final String[] words = valueText.split(" ");
                        final int wordCount = words.length;

                        // Start at index 0, in case valueText starts with space(s)
                        for (int k = 0; k < wordCount; k++) {
                            if (words[k].startsWith(prefixString)) {
                                newValues.add(value);
                                break;
                            }
                        }
                    }
                }

                results.values = newValues;
                results.count = newValues.size();
            }

            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            //noinspection unchecked
            mObjects = (List<T>) results.values;
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    }

 所以,我的做法就是,直接复制整个ArrayAdapter类,然后将里面的那句原则改掉,改成  valueText.contains(prefixString)  ,这样在设置的时候设自定义的这个类,然后,然后就能看到全局匹配的AutoCompleteTextView了。

你可能感兴趣的:(AutoCompleteTextView全局匹配)