首页 > 代码库 > BaseAdapter的优化

BaseAdapter的优化

技术分享
 1 package cct.commonadapter.bean;
 2 import android.content.Context;
 3 import android.view.LayoutInflater;
 4 import android.view.View;
 5 import android.view.ViewGroup;
 6 import android.widget.BaseAdapter;
 7 import android.widget.TextView;
 8 import java.util.List;
 9 import cct.commonadapter.R;
10 
11 public class MyAdapter extends BaseAdapter {
12     private LayoutInflater inflater;
13     private List<Message> mdata;
14     public MyAdapter(Context context,List<Message> mdata){
15          inflater=LayoutInflater.from(context);
16         this.mdata=http://www.mamicode.com/mdata;
17     }
18     @Override
19     public int getCount() {
20         return mdata.size();
21     }
22     @Override
23     public Object getItem(int position) {
24         return mdata.get(position);
25     }
26     @Override
27     public long getItemId(int position) {
28         return position;
29     }
30     @Override
31     public View getView(int position, View convertView, ViewGroup parent) {
32           //管理所有属性的一个类
33         ViewHolder holder;
34         if(convertView==null){
35           //创建一个视图对象
36             convertView=inflater.inflate(R.layout.item,parent,false);
37             holder=new ViewHolder();
38             holder.mtitle= (TextView) convertView.findViewById(R.id.mtitle);
39             holder.mdesc= (TextView) convertView.findViewById(R.id.mdesc);
40             holder.mtime= (TextView) convertView.findViewById(R.id.mtime);
41             holder.mphone= (TextView) convertView.findViewById(R.id.mphone);
42             //绑定ViewHolder对象
43             convertView.setTag(holder);
44         }
45         else {
46            //取出ViewHolder对象
47             holder = (ViewHolder) convertView.getTag();
48         }
49             Message ms = mdata.get(position);
50       /*设置TextView显示的内容,即我们存放在动态数组中的数据*/
51             holder.mtitle.setText(ms.getTitle());
52             holder.mdesc.setText(ms.getDesc());
53             holder.mtime.setText(ms.getTime());
54             holder.mphone.setText(ms.getPhone());
55            return convertView;
56     }
57     private  class ViewHolder
58     {
59         TextView mtitle;
60         TextView mdesc;
61         TextView mtime;
62         TextView mphone;
63     }
64 }
View Code

 

BaseAdapter的优化