android - Change AutoCompleteTextView filter from "startsWith" to "Contains"? -


i change default filtering in autocompletetextview. default filtering finds strings startswith given token. project requires filtering should find strings contains given token.

is possible?

i found solution that, google , searching 2 days. @torque203 suggested, i've implemented own custom adapter. first define new xml file custom item in adapter:

autocomplete_item.xml

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="match_parent"     android:layout_height="match_parent">      <textview         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:textappearance="?android:attr/textappearancemedium"         android:text="medium text"         android:paddingleft="8dp"         android:paddingright="8dp"         android:paddingtop="16dp"         android:paddingbottom="16dp"         android:id="@+id/lbl_name" /> </relativelayout> 

create new class names:

names

public class names {     public string name; } 

namesadapter

public class namesadapter extends arrayadapter<names> {      context context;     int resource, textviewresourceid;     list<names> items, tempitems, suggestions;      public namesadapter(context context, int resource, int textviewresourceid, list<names> items) {         super(context, resource, textviewresourceid, items);         this.context = context;         this.resource = resource;         this.textviewresourceid = textviewresourceid;         this.items = items;         tempitems = new arraylist<names>(items); // makes difference.         suggestions = new arraylist<names>();     }      @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(r.layout.autocomplete_item, parent, false);         }         names names = items.get(position);         if (names != null) {             textview lblname = (textview) view.findviewbyid(r.id.lbl_name);             if (lblname != null)                 lblname.settext(names.name);         }         return view;     }      @override     public filter getfilter() {         return namefilter;     }      /**      * custom filter implementation custom suggestions provide.      */     filter namefilter = new filter() {         @override         public charsequence convertresulttostring(object resultvalue) {             string str = ((names) resultvalue).name;             return str;         }          @override         protected filterresults performfiltering(charsequence constraint) {             if (constraint != null) {                 suggestions.clear();                 (names names : tempitems) {                     if (names.name.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<names> filterlist = (arraylist<names>) results.values;             if (results != null && results.count > 0) {                 clear();                 (names names : filterlist) {                     add(names);                     notifydatasetchanged();                 }             }         }     }; } 

searchactivity (or main activity)

....    list<names> nameslist =  //your names list;    namesadapter namesadapter = new namesadapter(                     searchactivity.this,                     r.layout.activity_search,                     r.id.lbl_name,                     nameslist             );             //set adapter liststudent             autocompletetextview.setadapter(namesadapter);             autocompletetextview.showdropdown(); ... 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -