Today in class, the teacher used the Simpleadapter, then the goddess on the side asked me why this simpleadapter can not do my app that with progress bar effect, speech is not sure, and then began to look at the source code, found that the implementation of this adapter really should be well researched, harvest or a lot of.
First look at the constructor, the constructor passed in a data source list<map>, a layout file ID (Resource), a string array is the corresponding key inside the map, and an integer array corresponding to the ID of each control in the item layout
Public Simpleadapter (context context, list<? extends Map<string,?>> data, @LayoutRes int resource, String[] From, @IdRes int[] to) { mdata = data; Mresource = Mdropdownresource = resource; Mfrom = from; MTo = to; Minflater = (layoutinflater) context.getsystemservice (Context.layout_inflater_service); }
Thus, the implementation of this adapter is to take the from array as a key, and then according to the to array to get each item's control ID, the list of map of each data map to item.
Next, we define several important methods that we need to rewrite when we customize adapter, GetCount is used to calculate how many Item,getitem are getting the corresponding data based on location, Getitemid is to get the ID of each item based on location , which requires that we get the relevant parameters from the data source.
Public int GetCount () { return mdata.size (); }
Public Object getItem (int position) { return mdata.get (position); }
Public long getitemid (int position) { return position; }
And then there's the whole adapter important way. GetView, look at the code first.
public View getView (int position, View Convertview, ViewGroup parent) { return Createviewfromresource (Minflater, Position, Convertview, parent, mresource); }
Private int position, View Convertview, int Resource) { View v; if NULL ) { false); Else { = convertview; } BindView (position, v); return v; }
Private voidBindView (intposition, view view) { FinalMap DataSet =Mdata.get (position); if(DataSet = =NULL) { return; } FinalViewbinder Binder =Mviewbinder; FinalString[] From =Mfrom; Final int[] to =MTo; Final intCount =to.length; for(inti = 0; I < count; i++) { FinalView v =View.findviewbyid (To[i]); if(V! =NULL) { FinalObject data =Dataset.get (From[i]); String text= Data = =NULL? "": Data.tostring (); if(Text = =NULL) {text= ""; } Booleanbound =false; if(Binder! =NULL) {bound=Binder.setviewvalue (v, data, text); } if(!bound) { if(Vinstanceofcheckable) { if(DatainstanceofBoolean) {((checkable) v). setchecked ((Boolean) data); } Else if(VinstanceofTextView) { //note:keep the instanceof TextView check at the bottom of these//IFS since a lot of views is textviews (e.g. checkboxes).Setviewtext ((TextView) v, text); } Else { Throw NewIllegalStateException (V.getclass (). GetName () + "should be bound to a Boolean, not a" +(Data==NULL? "<unknown type>": Data.getclass ())); } } Else if(VinstanceofTextView) { //note:keep the instanceof TextView check at the bottom of these//IFS since a lot of views is textviews (e.g. checkboxes).Setviewtext ((TextView) v, text); } Else if(VinstanceofImageView) { if(DatainstanceofInteger) {Setviewimage ((ImageView) v, (Integer) data); } Else{setviewimage ((ImageView) v, text); } } Else { Throw NewIllegalStateException (V.getclass (). GetName () + "isn't a" + "view that can be bounds by This Simpleadapter "); } } } } }
The GetView method calls directly a method called Createviewfromresource, which first determines whether Convertview is empty, and this convertview is a cached view, because we know that Each listview is actually only a few items back and forth, and when an item slides out of the screen, the item is recycled to generate the next displayed view, that is, the entire screen may be less than the size of the data, The purpose of the cache used here is that if the view has been generated before, then we can no longer re-map, directly can get back to use, this is a way to improve performance. Next, call a BindView method, in this method, according to the type of view obtained, to determine how the data should be mapped to the layout, for example, if the type of view is TextView, the value of the data source is set to its text, etc. Here can only be checkable, TextView and ImageView These three kinds of view to use, if have progressbar such, we can only define a adapter.
Android Development Learning Path-simpleadapter Source Analysis Learning