Android 簡單案例:繼承BaseAdapter實現Adapter

來源:互聯網
上載者:User

標籤:return   length   second   ram   sample   ati   span   ==   else   

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"),    };}

Android 簡單案例:繼承BaseAdapter實現Adapter

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.