码迷,mamicode.com
首页 > 其他好文 > 详细

适配器的经典写法

时间:2014-11-26 18:44:46      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   使用   sp   

class GridViewAdapter extends BaseAdapter{

        LayoutInflater inflater;
        List<PackageInfo> pkInfos;
        public GridViewAdapter(Context context,List<PackageInfo> packageInfos) {
            inflater = LayoutInflater.from(context);
            this.pkInfos = packageInfos;
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return pkInfos.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return pkInfos.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
        
                View view = inflater.inflate(R.layout.gv_item, null);
                TextView tv = (TextView)view.findViewById(R.id.gv_item_appname);
                ImageView iv = (ImageView)view.findViewById(R.id.gv_item_icon);
                tv.setText(pkInfos.get(position).applicationInfo.loadLabel(getPackageManager()));
                iv.setImageDrawable(pkInfos.get(position).applicationInfo.loadIcon(getPackageManager()));
                
            return view;
        }
        
    }

bubuko.com,布布扣

 

 1 class ListViewAdapter extends BaseAdapter{
 2 
 3         LayoutInflater inflater;
 4         List<PackageInfo> pkInfos;
 5         public ListViewAdapter(Context context,List<PackageInfo> packageInfos) {
 6             inflater = LayoutInflater.from(context);
 7             this.pkInfos = packageInfos;
 8         }
 9         @Override
10         public int getCount() {
11             // TODO Auto-generated method stub
12             return pkInfos.size();
13         }
14 
15         @Override
16         public Object getItem(int position) {
17             // TODO Auto-generated method stub
18             return pkInfos.get(position);
19         }
20 
21         @Override
22         public long getItemId(int position) {
23             // TODO Auto-generated method stub
24             return position;
25         }
26 
27         @Override
28         public View getView(int position, View convertView, ViewGroup parent) {
29             // TODO Auto-generated method stub
30         
31                 View view = inflater.inflate(R.layout.lv_item, null);
32                 TextView ntv = (TextView)view.findViewById(R.id.lv_item_appname);
33                 TextView ptv = (TextView)view.findViewById(R.id.lv_item_packagename);
34                 ImageView iv = (ImageView)view.findViewById(R.id.lv_icon);
35                 ntv.setText(pkInfos.get(position).packageName);
36                 ptv.setText(pkInfos.get(position).applicationInfo.loadLabel(getPackageManager()));
37                 iv.setImageDrawable(pkInfos.get(position).applicationInfo.loadIcon(getPackageManager()));
38                 
39             return view;
40         }
41         
42     }

bubuko.com,布布扣

尽量自己构建适配器,这样加载速度快,效率高。哪怕简单的也尽量不要使用SimpleAdapter

适配器的经典写法

标签:style   blog   http   io   ar   color   os   使用   sp   

原文地址:http://www.cnblogs.com/hixin/p/4123749.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!