zoukankan      html  css  js  c++  java
  • Android 简单案例:继承BaseAdapter实现Adapter

    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    /**
     * This class provides data as Views. It is designed to support both ListView and GridView by
     * changing a layout resource file to inflate.
     */
    public class MeatAdapter extends BaseAdapter {
    
        private final LayoutInflater mLayoutInflater;
        private final int mResourceId;
    
        /**
         * Create a new instance of {@link MeatAdapter}.
         *
         * @param inflater   The layout inflater.
         * @param resourceId The resource ID for the layout to be used. The layout should contain an
         *                   ImageView with ID of "meat_image" and a TextView with ID of "meat_title".
         */
        public MeatAdapter(LayoutInflater inflater, int resourceId) {
            mLayoutInflater = inflater;
            mResourceId = resourceId;
        }
    
        @Override
        public int getCount() {
            return Meat.MEATS.length;
        }
    
        @Override
        public Meat getItem(int position) {
            return Meat.MEATS[position];
        }
    
        @Override
        public long getItemId(int position) {
            return Meat.MEATS[position].resourceId;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final View view;
            final ViewHolder holder;
            if (null == convertView) {
                view = mLayoutInflater.inflate(mResourceId, parent, false);
                holder = new ViewHolder();
                holder.image = (ImageView) view.findViewById(R.id.meat_image);
                holder.title = (TextView) view.findViewById(R.id.meat_title);
                view.setTag(holder);
            } else {
                view = convertView;
                holder = (ViewHolder) view.getTag();
            }
            Meat meat = getItem(position);
            holder.image.setImageResource(meat.resourceId);
            holder.title.setText(meat.title);
            return view;
        }
    
        private static class ViewHolder {
            public ImageView image;
            public TextView title;
        }
    
    }
    /**
     * Sample data.
     */
    public class Meat {
    
        public int resourceId;
        public String title;
    
        public Meat(int resourceId, String title) {
            this.resourceId = resourceId;
            this.title = title;
        }
    
        public static final Meat[] MEATS = {
                new Meat(R.drawable.p1, "First"),
                new Meat(R.drawable.p2, "Second"),
                new Meat(R.drawable.p3, "Third"),
                new Meat(R.drawable.p4, "Fourth"),
                new Meat(R.drawable.p5, "Fifth"),
                new Meat(R.drawable.p6, "Sixth"),
                new Meat(R.drawable.p7, "Seventh"),
                new Meat(R.drawable.p8, "Eighth"),
                new Meat(R.drawable.p9, "Ninth"),
                new Meat(R.drawable.p10, "Tenth"),
                new Meat(R.drawable.p11, "Eleventh"),
        };
    
    }
  • 相关阅读:
    新版ubuntu中打开终端的方法和安装ssh 的方法
    HTML中利用404将老域名重定向到新域名
    KeelKit 1.0.3500.25185
    如何制作VSPackage的安装程序
    一副漫画:IE6滚回你老家去
    “表单控件”与“实体类”
    VS2005中得到 Web页面 或 窗体的 IDesignerHost
    一句SQL搞定分页
    CodeDom Assistant CodeDom的强大工具, 有些BUG修正了下,发到CodePlex,大家有需要的可以看看
    VS2005 出现 The OutputPath property is not set for this project. 错误的解决方法
  • 原文地址:https://www.cnblogs.com/onelikeone/p/7586889.html
Copyright © 2011-2022 走看看