编写自己的Adapter模板

阅读更多

如果你项目一直用系统给你封装的BaseAdapter的话,那么我只想说,你不累么?
代码繁多还要写数据缓存,还不如自己动手写一个模板吧,这样后面项目就可以直接套用了,编写和执行效率大大提升啊。
BaseAdapter.java

001 import java.util.ArrayList;
002 import java.util.Collection;
003 import java.util.Collections;
004 import java.util.Comparator;
005 import java.util.List;
006  
007 import android.content.Context;
008 import android.content.res.Resources;
009 import android.view.LayoutInflater;
010 import android.view.View;
011 import android.view.ViewGroup;
012  
013  
014 /**
015  * 实现这个适配器模板会少很多代码
016  * @author leehom
017  * @param
018  */
019 public abstract class BaseAdapter extends android.widget.BaseAdapter {
020  
021     protected List list;
022     protected Context context;
023     protected LayoutInflater inflater;
024     private int layoutID;
025  
026     public BaseAdapter(Context context, T[] ts, int layoutID) {
027         super();
028         this.context = context;
029         this.list = new ArrayList();
030         this.layoutID = layoutID;
031         Collections.addAll(list, ts);
032         this.inflater = LayoutInflater.from(context);
033     }
034  
035     public BaseAdapter(Context context, List list, int layoutID) {
036         super();
037         this.layoutID = layoutID;
038         this.context = context;
039         this.list = new ArrayList();
040         this.list.addAll(list);
041         this.inflater = LayoutInflater.from(context);
042     }
043  
044     public Resources getResources() {
045         return context.getResources();
046     }
047  
048     public synchronized void add(T object) {
049         list.add(object);
050     }
051  
052     public synchronized void addAll(Collectionextends T> collection) {
053         list.addAll(collection);
054     }
055  
056     public synchronized void remove(T object) {
057         list.remove(object);
058     }
059  
060     public synchronized void insert(T object, int index) {
061         list.add(index, object);
062     }
063  
064     public synchronized void clear() {
065         list.clear();
066     }
067  
068     public synchronized void sort(Comparatorsuper T> comparator) {
069         Collections.sort(list, comparator);
070     }
071  
072     @Override
073     public T getItem(int position) {
074         // TODO Auto-generated method stub
075         return list.get(position);
076     }
077  
078     @Override
079     public int getCount() {
080         // TODO Auto-generated method stub
081         return list.size();
082     }
083  
084     @Override
085     public long getItemId(int position) {
086         // TODO Auto-generated method stub
087         return position;
088     }
089  
090     @Override
091     public View getView(int position, View convertView, ViewGroup parent) {
092         // TODO Auto-generated method stub
093         ViewHolder holder = null;
094         if (convertView == null) {
095             convertView = inflater.inflate(layoutID, null);
096             holder = new ViewHolder(convertView);
097             initView(holder);
098             convertView.setTag(holder);
099         } else {
100             holder = (ViewHolder) convertView.getTag();
101         }
102         setViewValue(holder, position);
103         return convertView;
104     }
105  
106     public List getList() {
107         return list;
108     }
109      
110     /**
111      * 向ViewHolder类里面添加View
112      * @param holder
113      */
114     public abstract void initView(ViewHolder holder);
115  
116     /**
117      * 从ViewHolder获取对应ID的View设置其值
118      * @param holder
119      * @param t 数据对象
120      */
121     public abstract void setViewValue(ViewHolder holder, int position);
122  
123 }

用的时候也是继承它,然后只需要初始化ID和设置Value就行了。例:

01 import java.lang.reflect.Field;
02 import java.lang.reflect.InvocationTargetException;
03 import java.lang.reflect.Method;
04 import java.util.ArrayList;
05  
06 import android.annotation.SuppressLint;
07 import android.content.Context;
08  
09 import com.fenjin.app.humiture.R;
10 import com.fenjin.app.item.DefaultItem;
11  
12 @SuppressLint("DefaultLocale")
13 public class DefaultAdapter extends BaseAdapter {
14  
15     public DefaultAdapter(Context context, Object object) {
16         super(context, new ArrayList(),
17                 R.layout.default_list_item_layout);
18         // TODO Auto-generated constructor stub
19         try {
20             Field[] fields = object.getClass().getDeclaredFields();
21             for (Field field : fields) {
22                 String lable = field.getName();
23                 String firstWord = lable.substring(0, 1).toUpperCase();
24                 StringBuffer buffer = new StringBuffer("get");
25                 buffer.append(firstWord);
26                 buffer.append(lable.substring(1));
27                 String name = buffer.toString();
28                 Method method = object.getClass().getDeclaredMethod(name);
29                 String value = method.invoke(object).toString();
30                 DefaultItem item = new DefaultItem(lable, value);
31                 add(item);
32             }
33         } catch (IllegalArgumentException e) {
34             // TODO Auto-generated catch block
35             e.printStackTrace();
36         } catch (NoSuchMethodException e) {
37             // TODO Auto-generated catch block
38             e.printStackTrace();
39         } catch (IllegalAccessException e) {
40             // TODO Auto-generated catch block
41             e.printStackTrace();
42         } catch (InvocationTargetException e) {
43             // TODO Auto-generated catch block
44             e.printStackTrace();
45         }
46     }
47  
48     @Override
49     public void initView(ViewHolder holder) {
50         // TODO Auto-generated method stub
51         holder.addView(R.id.default_lable);
52         holder.addView(R.id.default_value);
53     }
54  
55     @Override
56     public void setViewValue(ViewHolder holder, int position) {
57         // TODO Auto-generated method stub
58         DefaultItem item = getItem(position);
59         holder.getTextView(R.id.default_lable).setText(item.getLable());
60         holder.getTextView(R.id.default_value).setText(item.getValue());
61     }
62  
63     @Override
64     public boolean isEnabled(int position) {
65         // TODO Auto-generated method stub
66         return false;
67     }
68  
69 }

javabean代码

01 public class DefaultItem {
02  
03     String lable;
04     String value;
05  
06     public DefaultItem(String lable, String value) {
07         super();
08         this.lable = lable;
09         this.value = value;
10     }
11  
12     public String getLable() {
13         return lable;
14     }
15  
16     public void setLable(String lable) {
17         this.lable = lable;
18     }
19  
20     public String getValue() {
21         return value;
22     }
23  
24     public void setValue(String value) {
25         this.value = value;
26     }
27  
28 }

xml代码

01 xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03     android:layout_width="match_parent"
04     android:layout_height="match_parent"
05     android:orientation="horizontal"
06     android:padding="@dimen/margin_10" >
07  
08     <TextView
09         android:id="@+id/default_lable"
10         android:layout_width="wrap_content"
11         android:layout_height="wrap_content" />
12  
13     <TextView
14         android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
16         android:text=":" />
17  
18     <TextView
19         android:id="@+id/default_value"
20         android:layout_width="wrap_content"
21         android:layout_height="wrap_content" />
22  
23 LinearLayout>

每次实现的代码仅仅这么点

01 @Override
02 public void initView(ViewHolder holder) {
03     // TODO Auto-generated method stub
04     holder.addView(R.id.default_lable);
05     holder.addView(R.id.default_value);
06 }
07  
08 @Override
09 public void setViewValue(ViewHolder holder, int position) {
10     // TODO Auto-generated method stub
11     DefaultItem item = getItem(position);
12     holder.getTextView(R.id.default_lable).setText(item.getLable());
13     holder.getTextView(R.id.default_value).setText(item.getValue());
14 }

是不是非常不错呢?

你可能感兴趣的:(android,java)