java - Search in listview using edittext -
i need search item in listview.i have easy array of string , array adapter,put code wich find @ different questions , work!but have 1 problem,this search slow , lags 1 letter.i have word "arbuz"(one many others) , print a-nothing,some letter-and words search "a",another-and no words this.i hope unterstand me.how can improve search? code:
public class fragmentwithlist extends listfragment { listview mainlist; edittext inputsearch; public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.fragment_with_list, null); mainlist = (listview) v.findviewbyid(android.r.id.list); inputsearch = (edittext)v.findviewbyid(r.id.inputsearch); return v; } public void oncreate(bundle savedinstancestate){ sethasoptionsmenu(true); super.oncreate(savedinstancestate); } public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); string values[] = new string[]{"arbuz","abrikos","banan","cup","charlie","derevo","grusha","ogurets","sliva","jabloko","volk","dom","igra","pharaon","muscle"}; comparator<string> alphabetical_order1 = new comparator<string>() { public int compare(string object1, string object2) { int res = string.case_insensitive_order.compare(object1.tostring(), object2.tostring()); return res; } }; collections.sort(arrays.aslist(values), alphabetical_order1); final arrayadapter<string> adapter = new arrayadapter<string>(getactivity(),android.r.layout.simple_list_item_1,values); mainlist.setadapter(adapter); inputsearch.addtextchangedlistener(new textwatcher() { @override public void beforetextchanged(charsequence s, int start, int count, int after) { adapter.getfilter().filter(s.tostring()); } @override public void ontextchanged(charsequence s, int start, int before, int count) { } @override public void aftertextchanged(editable s) { } }); } }
add following code in aftertextchanged()
method
adapter.getfilter().filter(s.tostring());
Comments
Post a Comment