Android中的Adapter简单介绍
时间:2023-01-22 10:01:51
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,系统被自动加载。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java长整除问题浅谈
java编码IDEA主题推荐
![](https://img.aspxhome.com/file/2023/6/85506_0s.jpg)
一篇文章弄懂Java和Kotlin的泛型难点
SpringDataRedis入门和序列化方式解决内存占用问题小结
![](https://img.aspxhome.com/file/2023/9/84249_0s.jpg)
Jetpack navigation组件超详细讲解
![](https://img.aspxhome.com/file/2023/6/125536_0s.png)
Android开发之App widget用法实例分析
java保证对象在内存中唯一性的实现方法
C# 删除字符串中的中文(实例分享)
spring security获取用户信息的实现代码
Android实现拍照或者选取本地图片
![](https://img.aspxhome.com/file/2023/6/120066_0s.jpg)
springboot自定义过滤器的方法
JavaWeb Servlet实现文件上传与下载功能实例
![](https://img.aspxhome.com/file/2023/2/62532_0s.png)
SpringBoot接口如何统一异常处理
![](https://img.aspxhome.com/file/2023/9/84309_0s.png)
Spring IOC:CreateBean环节中的流程转换
![](https://img.aspxhome.com/file/2023/3/64903_0s.jpg)
Spring事务事件监控的实现
Unity 如何获取鼠标停留位置下的物体
FileShare枚举的使用小结(文件读写锁)
![](https://img.aspxhome.com/file/2023/0/100210_0s.png)
Java中的BufferedInputStream与BufferedOutputStream使用示例
浅谈Java中几种常见的比较器的实现方法
Servlet3.0学习总结之基于Servlet3.0的文件上传实例
![](https://img.aspxhome.com/file/2023/1/58141_0s.gif)