I believe that one of the scenarios in the mobile phone address book is that the data in the text box can be automatically filtered when the user selects the address book. I was also invited by a netizen today, so I sorted out the knowledge points here. Now I want to share with you some questions that can be shared with me. The implementation result is as follows.
In fact, we believe that you will be familiar with another control, so we can use autocompletetextview. You may find the answer you need by looking at the source code of this control. The core here is a filterable. As for the introduction and usage of filterable, you can search for related knowledge on your own. There are two important methods.
|
Method Name |
Function |
|
Protected filterresults extends mfiltering (charsequence prefix) |
Execute the filter method in this method |
|
Protected
VoidPublishresults (charsequence constraint,
Filterresults results) |
In this method, the data obtained after filtering is published and the adapter is updated. |
To understand this knowledge, let's look at the core code. Here we will rewrite the baseadapter and then implement filterable.
Public class useradapter extends baseadapter implements filterable {private myfilter; private list <userinfo> userinfos; private context; private arraylist <userinfo> moriginalvalues; private final object mlock = new object (); public useradapter (context, list <userinfo> userinfos) {This. context = context; this. userinfos = userinfos;} @ overridepublic int getcount () {// todo auto-gener Ated method stubreturn userinfos. size () ;}@ overridepublic object getitem (INT arg0) {// todo auto-generated method stubreturn userinfos. get (arg0) ;}@ overridepublic long getitemid (INT position) {// todo auto-generated method stubreturn position ;}@ overridepublic view getview (INT position, view convertview, viewgroup parent) {view = convertview; viewholder holder; If (view = NULL) {view = layoutinfl Ater. from (context ). inflate (R. layout. list_item, null); holder = new viewholder (); holder. TV _nick = (textview) view. findviewbyid (R. id. TV _nick); holder. TV _mobile = (textview) view. findviewbyid (R. id. TV _mobile); view. settag (holder);} else {holder = (viewholder) view. gettag ();} holder. TV _nick.settext (userinfos. get (position ). getUserName (); holder. TV _mobile.settext (userinfos. get (position ). getphonenum (); Return View;} static class viewholder {textview TV _nick; textview TV _mobile;} @ overridepublic filter getfilter () {If (myfilter = NULL) {myfilter = new myfilter ();} return myfilter;} class myfilter extends filter {@ overrideprotected filterresults expect mfiltering (charsequence prefix) {// hold the data after the filtering operation is complete. This data includes the value and quantity of data after the filter operation. Count: the number of values contains the value of filterresults Results = new filterresults (); If (moriginalvalues = NULL) {synchronized (mlock) {// convert the list user set to the arraylistmoriginalvalues = new arraylist <userinfo> (userinfos);} If (prefix = NULL | prefix. length () = 0) {synchronized (mlock) {arraylist <userinfo> List = new arraylist <userinfo> (moriginalvalues); results. values = List; results. count = List. size () ;}} else {// perform formal filtering string prefixstring = prefix. tostring (). tolowercase (); // declare a temporary collection object to assign the original data to this temporary variable final arraylist <userinfo> values = moriginalvalues; Final int COUNT = values. size (); // The new set object final arraylist <userinfo> newvalues = new arraylist <userinfo> (count); For (INT I = 0; I <count; I ++) {// if the name prefix matches or the phone number matches, add it to the new set final userinfo value = (userinfo) values. get (I); log. I ("coder", "pinyinutils. getalpha (value. getUserName () "+ pinyinutils. getalpha (value. getUserName (); If (pinyinutils. getalpha (value. getUserName ()). startswith (prefixstring) | value. getphonenum (). startswith (prefixstring) | value. getUserName (). startswith (prefixstring) {newvalues. add (value) ;}// then assign the new set data to the filterresults object results. values = newvalues; results. count = newvalues. size () ;}return results ;}@ overrideprotected void publishresults (charsequence constraint, filterresults results) {// re-assign the list associated with the adapter to userinfos = (list <userinfo>) results. values; If (results. count> 0) {policydatasetchanged ();} else {policydatasetinvalidated ();}}}}
For specific codes that cannot be understood, you can view comments or add QQ
Finally, let's take a look at how it works.
et_filter.addTextChangedListener(new TextWatcher() {@Overridepublic void onTextChanged(CharSequence s, int start, int before,int count) {adapter.getFilter().filter(et_filter.getText().toString());}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count,int after) {// TODO Auto-generated method stub}@Overridepublic void afterTextChanged(Editable s) {// TODO Auto-generated method stub}});
Well, this is a simple function. I hope it will help you.
If you need to reprint reference please indicate the source: http://blog.csdn.net/jiahui524