博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 简单案例:继承BaseAdapter实现Adapter
阅读量:7073 次
发布时间:2019-06-28

本文共 2838 字,大约阅读时间需要 9 分钟。

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

转载于:https://www.cnblogs.com/onelikeone/p/7586889.html

你可能感兴趣的文章
openstack概述
查看>>
How To Detect Which Element Was Clicked, Using jQuery
查看>>
javascript & jQuery
查看>>
DW快速去除tppabs冗余代码
查看>>
Java8新特性之:新的日期和时间API
查看>>
如何才能从程序员成长为实战型架构师?必掌握这7大实战技能经验
查看>>
rabbitMQ集群的搭建和维护第二篇---利用python程序完成mq的消息收发和实时监控
查看>>
网众设置开机重启服务的命令,才可连接BOOT服务器
查看>>
数字签名基本原理
查看>>
RHEL6.3 DNS配置详解一 DNS相关概念理解及配置基础
查看>>
Windows环境 和 Linux环境下搭建Qt开发环境
查看>>
简述synchronized和java.util.concurrent.locks.Lock的异同
查看>>
辅DNS服务器部署文档(for linux平台)
查看>>
weblogic安装问题
查看>>
在win2008r2下开启ntp服务
查看>>
我的友情链接
查看>>
SpringMVC源码解析(三)——HandlerAdapter
查看>>
Python执行系统命令的方法
查看>>
动态加载远程Jar的实现方式
查看>>
无线***笔记(一)-《***WPA-PSK加密无线网络》
查看>>