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;
        }
    }

 

转载于:https://www.cnblogs.com/zhangshuli-1989/p/zhangshuli_myadapter_150318144.html

你可能感兴趣的:(adapter-自定义adapter的典型写法)