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

}
复制代码
posted @   行走的思想  阅读(688)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示