手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >Android中的Adapter简单介绍
Android中的Adapter简单介绍
摘要:Android中的Adapter在自定义显示列表时非常有用,比如SimpleAdapter,它的构造函数是:publicSimpleAdap...

Android中的Adapter在自定义显示列表时非常有用,比如SimpleAdapter,它的构造函数是:

public SimpleAdapter (Context context, List> data, int resource, String[] from, int[] to)

它的各参数的意思:

1.context,上下文,SimpleAdapter关联的视图,一般而言就是当前的Activity,this

2.data,泛型的List,如ArrayList,Map或者HashMap

3.resource,资源文件,一个R.layout,就是要显示的布局

4.from ,一个数组,Map中的键值对。

5.to,layout的xml文件中命名id形成的唯一的int型标识符

比如:

在一个ListActivity中定义一个List:

List> people= new ArrayList>();

Map m=new HashMap();

m.put("name","tom");

m.put("age","20");

people.add(m);

...

SimpleAdapter adapter = new SimpleAdapter(this,

(List>) feets, R.layout.main,

new String[] { "name","age" }, new int[] {R.id.name,R.id.age });

setListAdapter(adapter);

其中:

R.id.name,R.id.age 是在一个XML布局文件中定义的两个用于显示name和age的TextView。布局文件中要有一个ListView。或者在程序中定义也可以。

另外,注意在ListActivity中不需要设置setContentView,系统被自动加载。

【Android中的Adapter简单介绍】相关文章:

Android中库项目的使用方法图文介绍

基于Android AppWidgetProvider的使用介绍

Android中的Button自定义点击效果实例代码

Android手势识别简单封装类

Android 编程下字库的使用及注意事项

关于学习android中的.9.png的笔记

Android 简单的照相机程序的实例代码

android平台中的模拟器 hardWare 属性

Android开发之相对布局

Android 九宫格的实现方法

精品推荐
分类导航