Customize autoCompleteTextView to implement your own matching rules.

Source: Internet
Author: User

Anyone who has used autoCompleteTextView (auto) knows that it matches the string header by default.

Or if there is a space in the middle, it can also start from the space to match the subsequent string. At first glance, I thought it was an issue of auto matching rules,

It is actually a problem with the Adapter (ArrayAdapter was used originally ).

How can I customize matching rules ?!

1. Custom adapter inherits from baseAdapter

2. Implement the Filterable interface and find that there is a required method getFilter ().

3. It is learned that the getFilter () method must return an object of the Filter type.

4. Create an internal class that inherits the Filter in the custom adapter.

5. Data matching rules are implemented in the internal class.

For more information, see the code.

Public class myAdapter <T> extends BaseAdapter implements Filterable {private List <T> mOriginalValues; private List <T> mObject; private final Object mLock = new Object (); private int mResouce; private MyFilter myFilter = null; private LayoutInflater inflater; public myAdapter (Context context, int TextViewResouceId, List <T> objects) {init (context, TextViewResouceId, objects );} private void init (Context context, Int textViewResouceId, List <T> objects) {inflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE); mObject = objects; mResouce = textViewResouceId; myFilter = new MyFilter () ;}@ Overridepublic int getCount () {return mObject. size () ;}@ Overridepublic T getItem (int position) {return mObject. get (position) ;}@ Overridepublic long getItemId (int position) {return position ;}@ Overridepu Blic View getView (int position, View convertView, ViewGroup parent) {return getViewFromResouce (position, convertView, parent, mResouce);} private View getViewFromResouce (int position, View convertView, ViewGroup parent, int mResouce2) {if (convertView = null) {convertView = inflater. inflate (mResouce2, parent, false);} TextView TV = (TextView) convertView; T item = getItem (position); if (item instanceof CharSe Quence) {TV. setText (CharSequence) item);} else {TV. setText (item. toString ();} return TV;} // return Filter @ Overridepublic Filter getFilter () {return myFilter ;} // custom Filter private class MyFilter extends Filter {// Obtain the Filter result @ Overrideprotected FilterResults extends mfiltering (CharSequence constraint) {FilterResults results = new FilterResults (); if (mOriginalValues = null) {synchronized (mLock) {mOriginalValues = new ArrayList <T> (mObject) ;}} int count = mOriginalValues. size (); ArrayList <T> values = new ArrayList <T> (); for (int I = 0; I <count; I ++) {T value = mOriginalValues. get (I); String valueText = value. toString (); // custom matching rule if (valueText! = Null & constraint! = Null & valueText. contains (constraint) {values. add (value) ;}} results. values = values; results. count = values. size (); return results;} // publish the filter result @ SuppressWarnings ("unchecked") @ Overrideprotected void publishResults (CharSequence constraint, FilterResults results) {// assign the search result to mObject so that you do not need to search all strings each time you input the string. This improves the efficiency of mObject = (List <T>) results. values; if (results. count> 0) {policydatasetchanged ();} else {policydatasetinvalidated ();}}}}
Then create an auto control and use this adapter to fill it.


Customize autoCompleteTextView to implement your own matching rules.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.