Recently in a small project to use the search function, so on the Internet to find information, now recorded, so as not to forget later. A small example written when learning Android is written on the basis of
Reference Links:
Android implements A-Z alphabetical sorting and filtering search function for the ListView, enabling the kanji to be converted into pinyin
[Android] text box for search and erase effects
----------------------------------------------------------------------------------------
A ListView and Adapter fill data to dynamically change the display of the ListView based on what is entered
Mainactivity:
Private ListView LV; Private list<goods> lists=New arraylist<goods>(); Private EditText Et_search; Private Goodsadapter Adapter; Private Drawable Imgclear;
lv=(ListView) Findviewbyid (r.id.lv_goods); Et_search=(EditText) Findviewbyid (r.id.et_search); et_ Search.addtextchangedlistener (this); // Filter the Search Et_search.setontouchlistener (this) based on the change in input value of the entry box ; imgclear=new Goodsadapter (mainactivity. this, r.layout.goods_item,lists); Lv.setadapter (adapter);
Public voidFilterdata (String strdata) {List<Goods> filterlists=NewArraylist<goods>(); //If the input box is empty, all data is displayed if(Textutils.isempty (strdata)) {filterlists=lists; }Else{//Otherwise, the search results are displayedfilterlists.clear (); for(Goods list:lists) {String name=List.getname (); Filterlists.add (list); }} adapter.updateview ( This, filterlists); } //Filter search based on changes in input values@Override Public voidBeforetextchanged (Charsequence S,intStartintCount,intAfter ) { //when the value inside the input box is empty, update to the original list, otherwise filter the data listFilterdata (s.tostring ()); } @Override Public voidOnTextChanged (Charsequence S,intStartintBefore,intcount) {} @Override Public voidaftertextchanged (Editable s) {//The delete icon appears when the input box is not empty, and disappears when it is empty if(Textutils.isempty (s)) {et_search.setcompounddrawableswithintrinsicbounds (NULL, NULL, Imgclear,NULL); } } //Empty the input box@Override PublicBoolean OnTouch (View V, motioneventEvent) { Switch(Event. Getaction ()) { Casemotionevent.action_up:intCurX = (int)Event. GetX (); if(CurX > V.getwidth ()- -&&!Textutils.isempty (Et_search.gettext ())) {Et_search.settext (""); intCacheinputtype = Et_search.getinputtype ();//backup the input typeEt_search.setinputtype (Inputtype.type_null);//Disable Soft inputEt_search.ontouchevent (Event);//Call Native HandlerEt_search.setinputtype (Cacheinputtype);//Restore input type return true;//consume touch even } Break; } return false; }
Android edittext Input Box Search implementation