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的更多相关文章

  1. Android 简单案例:可移动的View

    CrossCompatibility.rar 1. VersionedGestureDetector.java import android.content.Context; import andro ...

  2. Android 简单案例:onSaveInstanceState 和 onRestoreInstanceState

    import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widg ...

  3. Android开发学习之路-自定义ListView(继承BaseAdapter)

    大三学生一个,喜欢编程,喜欢谷歌,喜欢Android,所以选择的方向自然是Android应用开发,开博第一篇,希望以后会有更多的进步. 最近在做一个记账App的时候,需要一个Activity来显示每个 ...

  4. Android继承BaseAdapter时要重写的函数的说明

    原文来自:http://www.2cto.com/kf/201405/299601.html,我自己做了一些修改 Android中继承BaseAdapter后需要重写四个函数,但一般还要写一个构造函数 ...

  5. 继承BaseAdapter实现Filterable的adapter类完整示例

    转载:http://www.lai18.com/content/1631130.html 目标:自定义ListView项布局通常需要自己实现Adapter,并通过搜索关键字筛选部分数据.且关键字变长变 ...

  6. [Design Pattern] Adapter Pattern 简单案例

    Adapter Pattern, 即适配器模式,用于连接两个不兼容的接口,属于结构类的设计模式. 或者叫做,转换器模式. 下面是一个转换器模式简单案例. 假设已有 AudioPlayer 专门播放 m ...

  7. android json解析及简单例子+Android与服务器端数据交互+Android精彩案例【申明:来源于网络】

    android json解析及简单例子+Android与服务器端数据交互+Android精彩案例[申明:来源于网络] android json解析及简单例子:http://www.open-open. ...

  8. Android日记-SimpleAdapter和BaseAdapter

    SimpleAdapter 这是一个简单的适配器,可以将静态数据映射到XML文件中定义好的视图.你可以指定由Map组成的List(比如ArrayList)类型的数据.在ArrayList中的每个条目对 ...

  9. Android ListView理解之BaseAdapter

    ListView是Android开发过程中较为常见的组件之一,它将数据以列表的形式展现出来.一般而言,一个ListView由以下三个元素组 成: 1.View,用于展示列表,通常是一个xml所指定的. ...

随机推荐

  1. Android——edittext的几个属性

    <EditText android:layout_columnSpan="2" android:hint="To" android:layout_grav ...

  2. Elastic-Job - 分布式定时任务框架

    Elastic-Job - 分布式定时任务框架 摘要 Elastic-Job是ddframe中dd-job的作业模块中分离出来的分布式弹性作业框架.去掉了和dd-job中的监控和ddframe接入规范 ...

  3. 【Hibernate步步为营】--核心对象+持久对象全析(二)

    上篇文章讨论了Hibernate的核心对象,在开发过程中经经常使用到的有JTA.SessionFactory.Session.JDBC,当中SessionFactory可以看做数据库的镜像,使用它可以 ...

  4. find 下参数的关系默认是and 一个参数多个选项可以用 -or

    [root@ob2 mytmp]# find -type f -name "*.html" -or -name "*.txt"./02.html./aa.htm ...

  5. Fork of LGPL version of JPedal

    https://github.com/on-site/JPedal —————————————————————————————————————————————————————————————————— ...

  6. spring配置事务 元素 "tx:annotation-driven" 的前缀 "tx" 未绑定

    在进行spring与mybatis整合时,启动项目报错,控制台提示“元素 "tx:annotation-driven" 的前缀 "tx" 未绑定”. 经过查找, ...

  7. spark学习系列

    转自: http://www.cnblogs.com/magj2006/p/4316264.html spark 系列文章汇总 源码导读 spark 源码导读1 从spark启动脚本开始 spark ...

  8. .net开发遇到的一个问题

    之前项目有个entity是写在Entity层的,相关的配置项也写死在程序里了,而且还是个static的配置,后来有了新需求,上峰指示要从CMS读取配置内容,大概是要在BLL实现,BLL依赖IBLL的I ...

  9. 关于Cocos2d-x的专属数据类型

    1.Size类定义的实例是一个有width和height属性的类 Size s = Size(44,52); 其中 s.width=44 s.height=52 2.Vec2是一个带有两个变量的(常量 ...

  10. jquery load ($.load) 事件用法与分析(转)

    首先我们需要清楚的是jquery load方法是对jQuery.ajax()进行封装以方便我们使用的一个方法,当我们需要处理较为复杂的逻辑时候,还是需要用到jQuery.ajax()这个比较全面的方法 ...