文章参考 http://www.cnblogs.com/mengdd/p/3254323.html

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.ImageView;
private class MyAdapter extends BaseAdapter {
/*
*加载量,一般一个类的列表,里面有自己的属性
*/ private LayoutInflater mInflater;
private List list;
public MyAdapter(Context context,List list) {
// Cache the LayoutInflate to avoid asking for a new one each time.
mInflater = LayoutInflater.from(context);
this.list=list;
} /**
* The number of items in the list is determined by the number of speeches
* in our array.
* 决定自己显示的item行数,一般直接返回容器的长度
* @see android.widget.ListAdapter#getCount()
*/
public int getCount() {
return list.length;
} /**
* Since the data comes from an array, just returning the index is
* sufficent to get at the data. If we were using a more complex data
* structure, we would return whatever object represents one row in the
* list.
*返回itme对象,一般直接返回list[position]
* @see android.widget.ListAdapter#getItem(int)
*/
public Object getItem(int position) {
return list[position];
} /**
* Use the array index as a unique id.
*返回item下标
* @see android.widget.ListAdapter#getItemId(int)
*/
public long getItemId(int position) {
return position;
} /**
* Make a view to hold each row.
*实现资源适配,这个是最关键
* @see android.widget.ListAdapter#getView(int, android.view.View,
* android.view.ViewGroup)
*/
public View getView(int position, View convertView, ViewGroup parent) {    ViewHolder holder; if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
//添加持有者
convertView.setTag(holder);
} else {
   // 得到持有者
holder = (ViewHolder) convertView.getTag();
} // Bind the data efficiently with the holder.
holder.text.setText(list[position]);
holder.icon.setImageResource((list[position]); return convertView;
} /**
*持有者模式创建的一个静态类,它其实承载的就是一些控件,省却了我们单独创建的麻烦。里面其实放置的就是我们自己布局里面需要设定的控件
**/
static class ViewHolder {
TextView text;
ImageView icon;
}
}

adapter-自定义adapter的典型写法的更多相关文章

  1. Android中利用ViewHolder优化自定义Adapter的典型写法

    利用ViewHolder优化自定义Adapter的典型写法 最近写Adapter写得多了,慢慢就熟悉了. 用ViewHolder,主要是进行一些性能优化,减少一些不必要的重复操作.(WXD同学教我的. ...

  2. 利用ViewHolder优化自定义Adapter的典型写法

    1 public class MarkerItemAdapter extends BaseAdapter { private Context mContext = null; private List ...

  3. android代码优化----ListView中自定义adapter的封装(ListView的模板写法)

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  4. Android中Spinner下拉列表(使用ArrayAdapter和自定义Adapter实现) .

    今天学习了Spinner组件,使用Spinner相当于从下拉列表中选择项目,下面演示一下Spinner的使用(分别使用ArrayAdapter和自定义Adapter实现) (一):使用ArrayAda ...

  5. Android ListView 自定义 Adapter

    自定义Adapter类 public class ListViewAdapter extends BaseAdapter { private static final String TAG = Mai ...

  6. android 自定义adapter和线程结合 + ListView中按钮滑动后状态丢失解决办法

    adapter+线程 1.很多时候自定义adapter的数据都是来源于服务器的,所以在获取服务器的时候就需要异步获取,这里就需要开线程了(线程池)去获取服务器的数据了.但这样有的时候adapter的中 ...

  7. 关于自定义adapter使用getApplicationContext()影响主题

    最近弄了一个东西,listview+switch构成界面 关于android自定义adapter,继承自baseadapter,发现界面的switch开关主题变了想要的是浅色的主题,但是却发现变成了深 ...

  8. Android中的普通对话框、单选对话框、多选对话框、带Icon的对话框、以及自定义Adapter和自定义View对话框详解

    对话框就是一个AlertDialog,但是一个简单的AlertDialog,我们却可以将它玩出许多花样来,下面我们就来一起总结一下AlertDialog的用法.看看各位童鞋在平时的工作中否都用到了Al ...

  9. 【转】Android自定义Adapter的ListView的思路及代码

    原文网址:http://www.jb51.net/article/37236.htm Android自定义Adapter的ListView的思路及代码,需要的朋友可以参考一下   在开发中,我们经常使 ...

  10. 关于自定义Adapter实现ListView的使用

    以下为使用BaseAdapter作扩展,自定义Adapter来使用ListView控件: 需要注意以下的几点: 1.自定义Adapter时,需要特别注意Adapter类中getView()方法覆盖,注 ...

随机推荐

  1. JS实现页面跳转 浏览器地址栏保持不变

    JS实现页面跳转 浏览器地址栏保持不变 在公司内部框架中,发现点击超链接,页面发生跳转,而浏览器地址栏URL始终保持不变.分析其实现机制,响应A标签onclick事件,通过Ajax向服务器端发送htt ...

  2. OPENCV(6) —— 角点检测

    图像特征的类型通常指边界.角点(兴趣点).斑点(兴趣区域).角点就是图像的一个局部特征,应用广泛.harris角点检测是一种直接基于灰度图像的角点提取算法,稳定性高,尤其对L型角点检测精度高,但由于采 ...

  3. c# 导出excel格式xlsx

    string sb="";//sql字符串 AttachmentConfigSection configSection = ConfigurationManager.GetSect ...

  4. Linux常用截图软件

    1.Gnome-screenshot 是一款 GNOME 截图工具,顾名思义,它是一款用来对整个屏幕.一个特定的窗口或者用户所定义一些其他区域进行捕获的工具.该工具提供了几个其他的功能,包括对所捕获的 ...

  5. 错误:created a ThreadLocal with key of type ……but failed to remove it when the web application was stopped. This is very likely to create a memory leak.

    tomcat reload显示错误:SEVERE: The web application [/Interceptor] created a ThreadLocal with key of type ...

  6. HDU 5384 Danganronpa (AC自己主动机模板题)

    题意:给出n个文本和m个模板.求每一个文本中全部模板出现的总次数. 思路:Trie树权值记录每一个模板的个数.对于每一个文本跑一边find就可以. #include<cstdio> #in ...

  7. express中的中间件理解

    什么是中间件 中间件是一个可访问请求对象(req)和响应对象(res)的函数,在 Express 应用的请求-响应循环里,下一个内联的中间件通常用变量 next 表示.中间件的功能包括: 执行任何代码 ...

  8. 简单理解javascript的闭包

    看过网上关于javascript的闭包的概念和分析,看完之后都是一头雾水,完全不懂,零度我本来就对于概念性的东西很烦躁,没办法,硬着头皮翻阅了很多的资料,总算理清了一点头绪,现在分享给大家,错误之处还 ...

  9. ubuntu系统配置WinQQ

    首先安装Wine sudo add-apt-repository ppa:wine/wine-builds sudo apt-get update sudo apt-get install wineh ...

  10. Codefroces Educational Round 26 837 C. Two Seals

    C. Two Seals time limit per test 1 second memory limit per test 256 megabytes input standard input o ...