The ListView control is often used in Android projects. The getView () method in the corresponding Adapter has a standard format, as shown below: copy code 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 = (LayoutInflater) 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); 10 holder. iv = (TextView) convertView. findViewById (R. id. iv); 11 12 convertView. setTag (holder); 13} else {14 holder = (ViewHolder) convertView. getTag (); 15} 16 final HashMap <String, Object> map = list. get (position); 17 18 holder. iv. setImageResource (Integer. valueOf (map. get ("iv "). toString () ); 19 holder. TV. setText (map. get ("TV "). toString (); 20 21 holder. btn. setOnClickListener (new View. onClickListener () {22 @ Override23 public void onClick (View v) {24 Toast. makeText (context, map. get ("btn "). toString (), Toast. LENGTH_SHORT ). show (); 25} 26}); 27 28 return convertView; 29} 30 31 class ViewHolder {32 Button btn; 33 ImageView iv; 34 TextView TV; 35 36} copy the code below to be broken (if you want to directly read the code, skip this section -_-!): Class ViewHolder..., if (null = convertView) {... and so on are required for every Adapter compilation. These codes have little to do with the business logic, and there is no need to write duplicate codes every time. Therefore, there is obviously room to simplify the code. Since we do not need to rewrite internal classes such as ViewHolder, move it to the parent class. However, if ViewHolder has different views in the actual project, store them in list. But how can we retrieve them if they are put in list? Using index? Obviously it's not a good method. It's not a Resource Id. Isn't it good to use it? Therefore, it is more appropriate to put Resource Id as the key in Map, but since int (Resource Id) is the key, it is natural to use SparseArray. And then move the if (null = converView)... code to the parent class. So ABaseAdapter was born. The Code is as follows: Copy code 1/*** 2 to simplify ViewHolder in BaseAdapter 3 * Created with IntelliJ IDEA. 4 * Author: wangjie email: tiantian.china.2@gmail.com 5 * Date: 14-4-2 6 * Time: 7 */8 public abstract class ABaseAdapter extends BaseAdapter {9 Context context; 10 11 protected ABaseAdapter (Context context) {12 this. context = context; 13} 14 15 protected ABaseAdapter () {16} 17 18/** 19 * various controls Cache 20 */21 class ViewHolder {22 public SparseArray <View> views = new SparseArray <View> (); 23 24/** 25 * specify the resId and type to obtain the corresponding view26 * @ param convertView27 * @ param resId28 * @ param <T> 29 * @ return30 */31 <T extends View> T obtainView (View convertView, int resId) {32 View v = views. get (resId); 33 if (null = v) {34 v = convertView. findViewById (resId); 35 views. put (resId, v); 36} 37 return (T) v; 38} 39 40} 41 42/** 43 * The method needs to be implemented by subclass, and the resource id44 * @ return45 */46 public abstract int itemLayoutRes () of the item layout needs to be returned (); 47 48 @ Override49 public View getView (int position, View convertView, ViewGroup parent) {50 ViewHolder holder; 51 if (null = convertView) {52 holder = new ViewHolder (); 53 convertView = LayoutInflater. from (context ). inflate (itemLayoutRes (), null); 54 convertView. setTag (holder); 55} else {56 holder = (ViewHo Lder) convertView. getTag (); 57} 58 return getView (position, convertView, parent, holder); 59} 60 61/** 62 * use this getView method to replace the original getView method, 63 * @ param position64 * @ param convertView65 * @ param parent66 * @ param holder67 * @ return68 */69 public abstract View getView (int position, View convertView, ViewGroup parent, viewHolder holder); 70 71} copy the code above: added an itemLayoutRes () abstract method, which is provided to the subclass implementation and returned to the item Layout Resource id, which is used to call the getView method. We can see that in the above Code, in the getView method of the system, we previously did what we did in the BaseAdapter implementation class (initialize converView and bind ViewHolder as the tag ), then, the getView method provided by the system is discarded, and the getView abstract method written by the system is called directly. This getView method is provided to sub-classes for implementation and serves the same purpose as the getView method of the system, however, this getView method carries a ViewHolder object, which can be used by subclasses to obtain controls in items. The usage is as follows. For example, if MyAdapter is created and ABaseAdapter is inherited, note that the constructor of the parent class must be called in the constructor: 1 public MyAdapter (Context context, list <HashMap <String, Object> list) {2 super (context); 3 this. list = list; 4}: The above "super (context);" must be called. Implement the itemLayoutRes () method and return the layout of the item: 1 @ Override2 public int itemLayoutRes () {3 return R. layout. implementation of the getView method in item; 4} is as follows: Copy code 1 @ Override 2 public View getView (int position, View convertView, ViewGroup parent, ViewHolder holder) {3 final HashMap <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 () {10 @ Override11 public void onClick (View v) {12 Toast. makeText (context, map. get ("btn "). toString (), Toast. LENGTH_SHORT ). show (); 13} 14}); 15 16 iv. setImageResource (Integer. valueOf (map. get ("iv "). toString (); 17 TV. setText (map. get ("TV "). toString (); 18 19 return convertView; 20} copy the code above: Call holder. the obtainView method can be used to obtain controls in items;