Android Simpleadapter Source Parsing

Source: Internet
Author: User

Simpleadapter:

 Public  class simpleadapter extends baseadapter implements filterable  {      Private int[] mTo;//point to the ID of the control in the layout example: R.id.btn    PrivateString[] Mfrom;//Data source, from the key inside the map    PrivateViewbinder Mviewbinder;//interface type, there is a Setviewvalue method for special type of control such as: drawable when the external initialization interface, implementation of specific methods    Privatelist<? Extends Map<string,?>> Mdata;//Map data source packaged with list    Private intMresource;//Layout    Private intMdropdownresource;//Resource files    PrivateLayoutinflater Minflater;//layoutinflater used to load the interface    PrivateSimplefilter Mfilter;//Filter    PrivateArraylist<map<string,?>> Munfiltereddata;///Simpleadapter initialization, assigning the passed-in parameter to the local corresponding variable     Public Simpleadapter(context context, list<? extends Map<string,?>> data,intResource, string[] from,int[] to) {mdata = data;          Mresource = Mdropdownresource = resource;          Mfrom = from;          MTo = to;      Minflater = (layoutinflater) context. Getsystemservice (Context.layout_inflater_service); }/* * ListView for each item requires adapter "Return a View" (GetView), * that is, when the ListView starts drawing, the system first calls the GetCount () function, which is worth the LIS according to his return The length of the Tview, * and then, based on this length, calls GetView () to draw each item of the ListView in a row. If your GetCount () return value is 0, the * list row is not displayed, and if you return 1, only one row is displayed. Returns a few lines to display.      What if we have thousands of tens of thousands of or more item to display? * Create a new view for each item? No way!!! In fact, Android has already cached these views, you can look at the following to understand, * in fact, there is a component called Recycler in Android * /       Public int GetCount() {returnMdata.size (); } PublicObjectGetItem(intPosition) {returnMdata.get (position); } Public Long Getitemid(intPosition) {returnPosition } PublicViewGetView(intPosition, View Convertview, ViewGroup parent) {//Call Createviewfromresource to optimize memory        returnCreateviewfromresource (position, Convertview, parent, mresource); }/ * * ListView mechanism here again, when the first data comes to GetView Convertview must be NULL, * then use Minflater.inflate (resource, parent, * false) to initialize a view to it, but when a screen slides to the end, the first item, slide out of the screen, then it will * from the bottom, then Convertview is not NULL, the advantage of this method is when Convertview is not NULL *      Without loading the layout, using Convertview directly saves a step, which is also the first step in the optimized ListView. */      PrivateViewCreateviewfromresource(intPosition, View Convertview, ViewGroup parent,intResource) {View V;if(Convertview = =NULL) {v = minflater.inflate (resource, parent,false); }Else{v = convertview; }//This method is the coreBindView (position, v);returnV } Public void Setdropdownviewresource(intResource) { This. Mdropdownresource = resource; }@Override       PublicViewGetdropdownview(intPosition, View Convertview, ViewGroup parent) {returnCreateviewfromresource (position, Convertview, parent, Mdropdownresource); }//The main function of this method is to assign values in the order of the controls in the to array, such as new Int[]{r.id.tv_name,r.id.tv_pwd}.     Private void BindView(intPosition, view view) {FinalMap DataSet = mdata.get (position);//Find the map data for the corresponding position location        //If you do not find the jump        if(DataSet = =NULL) {return; }/* * The externally implemented Viewbinder, assigned to local, Simpleadapter can be implemented: * Checkbox,checkedtextview,textview *, Image View, the data type is BOOLEAN,INTEGER,STIRNG, so the special data type is used to Viewbinder *, if it is not necessary to implement the external Viewbinder interface and the inside of the method */          FinalViewbinder binder = Mviewbinder;FinalString[] from = Mfrom;Final int[] to = mTo;Final intCount = To.length;//view.findviewbyid (To[i]), loop to find the control         for(inti =0; I < count; i++) {FinalView v = View.findviewbyid (to[i]);//If V is not empty, the corresponding data is found, and the object type is converted to string because the Boolean is pure and true or false in the map .            if(V! =NULL) {FinalObject data = Dataset.get (From[i]); String Text = data = =NULL?"": Data.tostring ();if(Text = =NULL) {text =""; }//Flag variable bound, judging externally there is no implementation of Viewbinder, if implemented, executes Binder.setviewvalue (V, data, text) and returns true if special conditions are met                Booleanbound =false;if(Binder! =NULL{bound = Binder.setviewvalue (V, data, text); }//If the IF (!bound) is satisfied then the bound is still false, the description is normal data                if(!bound) {//view V is not an instantiated type of checkable, the condition that satisfies may be checkbox,checkedtextview                    if(Vinstanceofcheckable) {//If the data type is Boolean, then the checkbox                        if(DatainstanceofBoolean) {((checkable) v). setchecked ((Boolean) data);//If not a checkbox, then the judgment is not inherited TextView Checkedtextview, is the word assignment, not just throws the exception}Else if(VinstanceofTextView) {Setviewtext ((TextView) v, text); }Else{Throw NewIllegalStateException (V.getclass (). GetName () +"should is bound to a Boolean, not a"+ (data = =NULL?"<unknown type>": Data.getclass ())); }//If it is not an instantiated type of checkable, determine if it is an instantiated type of TextView}Else if(VinstanceofTextView) {Setviewtext ((TextView) v, text);//Neither is the case, just judge whether the ImageView type}Else if(VinstanceofImageView) {//This only satisfies the data type int is R.drawable.ic, and the string type URL, if wants to implement the direct use Drawbale, must implement Viewbinder                        if(DatainstanceofInteger) {setviewimage ((ImageView) v, (Integer) data); }Else{Setviewimage ((ImageView) v, text); }                      }Else{Throw NewIllegalStateException (V.getclass (). GetName () +"is not a"+"view that can is bounds by this simpleadapter"); }                  }              }          }      } PublicViewbinderGetviewbinder() {returnMviewbinder; } Public void Setviewbinder(Viewbinder Viewbinder)      {mviewbinder = Viewbinder; } Public void Setviewimage(ImageView V,intValue) {V.setimageresource (value); } Public void Setviewimage(ImageView V, String value) {Try{V.setimageresource (Integer.parseint (value)); }Catch(NumberFormatException NFE)          {V.setimageuri (Uri.parse (value)); }      } Public void Setviewtext(TextView V, String text)      {V.settext (text); } PublicFilterGetFilter() {if(Mfilter = =NULL) {Mfilter =NewSimplefilter (); }returnMfilter; } Public Static  interface viewbinder {          BooleanSetviewvalue (view view, Object data, String textrepresentation); }//Here is a data filter, filter by prefix    Private  class simplefilter extends Filter {          @Override          protectedFilterresultsperformfiltering(charsequence prefix) {Filterresults results =NewFilterresults ();if(Munfiltereddata = =NULL) {Munfiltereddata =NewArraylist<map<string,?>> (Mdata); }if(prefix = =NULL|| Prefix.length () = =0) {arraylist<map<string,?>> list = Munfiltereddata;                  Results.values = list;              Results.count = List.size (); }Else{String prefixstring = prefix.tostring (). toLowerCase (); arraylist<map<string,?>> unfilteredvalues = Munfiltereddata;intCount = Unfilteredvalues.size (); arraylist<map<string,?>> newValues =NewArraylist<map<string,?>> (count); for(inti =0; I < count; i++) {map<string,?> h = unfilteredvalues.get (i);if(H! =NULL) {intlen = mto.length; for(intj =0; J < Len;                              J + +) {string str = (string) h.get (Mfrom[j]); string[] Words = Str.split (" ");intWordCount = Words.length; for(intK =0; K < WordCount; k++) {String word = words[k];if(Word.tolowercase (). StartsWith (prefixstring)) {Newvalues.add (h); Break; }}}}} res                  Ults.values = newValues;              Results.count = Newvalues.size (); }returnResults }//Finally here is the filter result via notifydatasetchanged () Update UI        @Override          protected void Publishresults(charsequence constraint, filterresults results) {//Noinspection uncheckedMdata = (list<map<string,?>>) results.values;if(Results.count >0) {notifydatasetchanged (); }Else{notifydatasetinvalidated (); }          }      }} }

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Simpleadapter Source Parsing

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.