From blog: http://www.cnblogs.com/tiantianbyconan/p/3642849.html
In Android projects, the ListView control is often used, while the corresponding adapter GetView () method is written in a standard form, as follows:
1 @Override 2 public View getView (int position, view Convertview, ViewGroup parent) {3 Viewholder holder; 4 if (null = = Convertview) {5 holder = new Viewholder (); 6 Layoutinflater minflater = (Layout Inflater) Context.getsystemservice (Context.layout_inflater_service); 7 Convertview = minflater.inflate (R.layout.item, NULL); 8 holder.btn = (Button) Convertview.findviewbyid (R.ID.BTN); 9 holder.tv = (TextView) Convertview.findviewbyid (r.id.tv); holder.iv = (TextView) convertview.f Indviewbyid (R.ID.IV); Convertview.settag (holder);}else{14 holder = (viewholder) CO Nvertview.gettag ();}16 final hashmap<string, object> map = list.get (position); Holde R.iv.setimageresource (Integer.valueof (Map.get ("IV"). ToString ())); Holder.tv.setText (Map.get ("TV"). ToString ()) ; Holder.btn.setOnClickListener (New View.onclicklIstener () {@Override23 public void OnClick (View v) {toast.maketext (context, Map.get ("Btn"). ToString (), Toast.length_short). Show ();}26}); return convertview;29 }30 class viewholder{32 Button btn;33 ImageView iv;34 TextView tv;35 36}
The following is a break (if you want to see the code directly, skip this section-_-! ):
That is, each time you write adapter you need to write class Viewholder ..., if (null = = Convertview) {... Wait a minute. This code has little to do with business logic, and it's not necessary to write duplicate code every time.
So there is obviously room for simplifying the code.
Since our requirement is not to repeat the internal classes such as Viewholder, move it to the parent class.
But viewholder in the actual project has a different view, then use list to store it, but put in the list, how to take out? With index? Obviously not a good way, not have resource ID this thing, through this can not be good? So it is appropriate to place the Resource ID key in the map, but since the INT (Resource ID) is key, it is natural to think of using Sparsearray.
Then put if (null = = Converview) ... All of this code is moved to the parent class.
So Abaseadapter was born, the code is as follows:
1/** 2 * simplifies viewholder related to baseadapter 3 * Created with IntelliJ idea. 4 * Author:wangjie email:[email protected] 5 * date:14-4-2 6 * Time: PM 5:54 7 */8 Public abstract class Abase Adapter extends baseadapter{9 context context;10 one protected abaseadapter (context context) {This.cont ext = context;13}14 protected abaseadapter () {16}17 18/**19 * Cache of individual controls */21 public cl viewholder{22 public sparsearray<view> views = new sparsearray<view> (); 23 24/**25 * Specify RESID and type to get to the corresponding view26 * @param convertView27 * @param resId28 * @param <t>29 * @return30 */31 public <t extends view> T obtainview (View convertview, int resId) {32 View v = views.get (resId), if (null = = v) {-V = Convertview.findviewbyid (resId); 35 Views.put (ResId, v);}37 return (T) v;38}39 40}41 42/**43 * method requires subclass implementation, need to return the item layout of resource ID44 * @return45 */46 Public abstract int itemlayoutres (); @Override49 public View getView (int position, view Convertview, ViewGroup paren T) {Viewholder holder;51 if (null = = Convertview) {holder = new Viewholder (); 53 Convertview = Layoutinflater.from (context). Inflate (Itemlayoutres (), null); Convertview.settag (holder); 55 }else{56 holder = (viewholder) convertview.gettag ();}58 return GetView (position, con Vertview, parent, Holder); 59}60 61/**62 * Using this getview method to replace the original GetView method requires subclasses to implement the @param Position64 * @param convertView65 * @param parent66 * @param holder67 * @return68 */69 public abstract View GetView (int position, View Convertview, viewgroup parent, Viewholder Holder); 70 71}
As above code: Adds an abstract method of Itemlayoutres (), which is provided to the subclass implementation, returns the resource ID of the item layout, and provides the call for the subsequent GetView method.
You can see the code above, in the system's GetView method, do what we used to do in the Baseadapter implementation class (Initialize the Converview, and bind Viewholder as tag), and then discard the system-provided GetView method, Go directly to call oneself write GetView abstract method, this GetView method is provided to subclass to implement, function is same as System GetView, but this getview method carries a Viewholder object, This object can be used by subclasses to get the control in item.
Use the following methods, such as creating a myadapter and inheriting the Abaseadapter:
Note that the constructor of the parent class needs to be called in the constructor method:
1 Public myadapter (context context, list
That is, above the "super (context);" Must be called.
The Itemlayoutres () method is then implemented to return the item's layout:
1 @Override2 public int itemlayoutres () {3 return r.layout.item;4}
The implementation in the GetView method is as follows:
1 @Override 2 public View getView (int position, view Convertview, ViewGroup parent, Viewholder holder) {3 final HASHM ap<string, object> map = list.get (position); 4 5 Button btn = Holder.obtainview (Convertview, r.id.item_btn); 6 ImageView IV = Holder.obtainview ( Convertview, R.id.item_iv); 7 TextView TV = Holder.obtainview (Convertview, R.ID.ITEM_TV); 8 9 Btn.setonclicklistener (new View.onclicklistener () { @Override11 public void OnClick (View v) { Toast.maketext (context, Map.get (" Btn "). ToString (), Toast.length_short). Show (); }14 }); Iv.setimageresource (Integer.valueof ( Map.get ("IV"). ToString ())); Tv.settext (Map.get ("TV"). ToString ()); return convertview;20 }
As above code: Call the Holder.obtainview method to get the control in item;