zoukankan      html  css  js  c++  java
  • adapter-自定义adapter的典型写法

    文章参考 http://www.cnblogs.com/mengdd/p/3254323.html

    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.TextView;
    import android.widget.ImageView;  
        private class MyAdapter extends BaseAdapter {
          /*
           *加载量,一般一个类的列表,里面有自己的属性
          */
    
            private LayoutInflater mInflater; 
            private List list;
            public MyAdapter(Context context,List list) {
                // Cache the LayoutInflate to avoid asking for a new one each time.
                mInflater = LayoutInflater.from(context); 
                this.list=list;
             }
    
            /**
             * The number of items in the list is determined by the number of speeches
             * in our array.
             * 决定自己显示的item行数,一般直接返回容器的长度
              * @see android.widget.ListAdapter#getCount()
             */
            public int getCount() {
                return list.length;
            }
    
            /**
             * Since the data comes from an array, just returning the index is
             * sufficent to get at the data. If we were using a more complex data
             * structure, we would return whatever object represents one row in the
             * list.
             *返回itme对象,一般直接返回list[position] 
             * @see android.widget.ListAdapter#getItem(int)
             */
            public Object getItem(int position) {
                return list[position];
            }
    
            /**
             * Use the array index as a unique id.
             *返回item下标 
             * @see android.widget.ListAdapter#getItemId(int)
             */
            public long getItemId(int position) {
                return position;
            }
    
            /**
             * Make a view to hold each row.
             *实现资源适配,这个是最关键
             * @see android.widget.ListAdapter#getView(int, android.view.View,
             *      android.view.ViewGroup)
             */
            public View getView(int position, View convertView, ViewGroup parent) { 
    
               ViewHolder holder;
    
                 if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.list_item_icon_text, null); 
                    holder = new ViewHolder();
                    holder.text = (TextView) convertView.findViewById(R.id.text);
                    holder.icon = (ImageView) convertView.findViewById(R.id.icon); 
                    //添加持有者
                    convertView.setTag(holder);
                } else { 
                   // 得到持有者
                    holder = (ViewHolder) convertView.getTag();
                }
    
                // Bind the data efficiently with the holder.
                holder.text.setText(list[position]);
                holder.icon.setImageResource((list[position]);
    
                return convertView;
            }
    
    /**
    *持有者模式创建的一个静态类,它其实承载的就是一些控件,省却了我们单独创建的麻烦。里面其实放置的就是我们自己布局里面需要设定的控件
    **/
     static class ViewHolder {
                TextView text;
                ImageView icon;
            }
        }
  • 相关阅读:
    Delphi
    delphi trayIcon控件,如何实现窗口最小化的时候到系统托盘
    delphi2010自带 TTrayIcon 托盘图标控件使用方法
    通过例子来简单了解下TProgressBar的使用。 pas文件程序如下
    ORA-12154,ORA-12560解决过程
    博客备份小工具3
    各大招聘网站信息实时查询浏览
    IE7中使用Jquery动态操作name问题
    js问题杂记
    动态sql
  • 原文地址:https://www.cnblogs.com/zhangshuli-1989/p/zhangshuli_myadapter_150318144.html
Copyright © 2011-2022 走看看