ArrayAdapter、SimpleAdapter简单用法
1. 使用流程

2. ArrayAdapter
new ArrayAdapter<?>(context, textViewResourceId, objects)
context:上下文
textViewResourceId:列表项的布局文件id
objects:数据源(数组或集合)
public class MainActivity extends Activity {
private ListView myListView;
private ArrayAdapter<String>arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//新建数据源
String[] data = {"初探ListView","初探ListView","初探ListView","初探ListView"};
//新建适配器并绑定数据源
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
//设置适配器
myListView = (ListView) findViewById(R.id.myListView);
myListView.setAdapter(arrayAdapter);
}
}
3. SimpleAdapter
new SimpleAdapter(context, data, resource, from, to)
context:上下文
data:List> data,数据源必须是一个泛型为Map<String, ?>的集合。其中,集合中的每一个Map对应ListView中的一项。
resource:列表项的布局文件id
from:Map中的键名
to:列表项的布局文件中的组件id
public class MainActivity extends Activity {
private ListView myListView;
private SimpleAdapter simpleAdapter;
private List<Map<String, Object>> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//新建数据源
data = new ArrayList<Map<String, Object>>();
for (int i = 0; i < 20; i++) {
Map<String, Object>map = new HashMap<String, Object>();
map.put("img", R.mipmap.ic_launcher);
map.put("text", "初识simpleAdapter");
data.add(map);
}
//新建适配器并绑定数据源
simpleAdapter = new SimpleAdapter(this, getData(), R.layout.item, new String[]{"img", "text"}, new int[]{R.id.img, R.id.text});
//设置适配器
myListView = (ListView) findViewById(R.id.myListView);
myListView.setAdapter(simpleAdapter);
}
}
ArrayAdapter、SimpleAdapter简单用法的更多相关文章
- listActivity和ExpandableListActivity的简单用法
http://www.cnblogs.com/limingblogs/archive/2011/10/09/2204866.html 今天自己简单的总结了listActivity和Expandable ...
- Android—— ListView 的简单用法及定制ListView界面
一.ListView的简单用法 2. 训练目标 1) 掌握 ListView 控件的使用 2) 掌握 Adapter 桥梁的作用 实现步骤: 1)首先新建一个项目, 并让ADT 自动帮我们创建好活动. ...
- CATransition(os开发之画面切换) 的简单用法
CATransition 的简单用法 //引进CATransition 时要添加包“QuartzCore.framework”,然后引进“#import <QuartzCore/QuartzCo ...
- jquery.validate.js 表单验证简单用法
引入jquery.validate.js插件以及Jquery,在最后加上这个插件的方法名来引用.$('form').validate(); <!DOCTYPE html PUBLIC " ...
- NSCharacterSet 简单用法
NSCharacterSet 简单用法 NSCharacterSet其实是许多字符或者数字或者符号的组合,在网络处理的时候会用到 NSMutableCharacterSet *base = [NSMu ...
- ArrayAdapter适配器的用法,模拟QQ发消息界面。
import java.util.ArrayList; import android.app.Activity; import android.content.Context; import andr ...
- [转]Valgrind简单用法
[转]Valgrind简单用法 http://www.cnblogs.com/sunyubo/archive/2010/05/05/2282170.html Valgrind的主要作者Julian S ...
- Oracle的substr函数简单用法
substr(字符串,截取开始位置,截取长度) //返回截取的字 substr('Hello World',0,1) //返回结果为 'H' *从字符串第一个字符开始截取长度为1的字符串 subst ...
- Ext.Net学习笔记19:Ext.Net FormPanel 简单用法
Ext.Net学习笔记19:Ext.Net FormPanel 简单用法 FormPanel是一个常用的控件,Ext.Net中的FormPanel控件同样具有非常丰富的功能,在接下来的笔记中我们将一起 ...
随机推荐
- Thread.join(), CountDownLatch、CyclicBarrier和 Semaphore区别,联系及应用
在java 1.5中,提供了一些非常有用的辅助类来帮助我们进行并发编程,比如CountDownLatch,CyclicBarrier和Semaphore,今天我们就来学习一下这三个辅助类的用法, 由于 ...
- Python公众号开发(二)—颜值检测
上篇文章,我们把自己的程序接入了微信公众号,并且能把用户发送的文本及图片文件原样返回.今天我们把用户的图片通过腾讯的AI平台分析后再返回给用户. 为了防止我的文章被到处转载,贴一下我的公众号[智能制造 ...
- [Swift]LeetCode85. 最大矩形 | Maximal Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...
- [Swift]LeetCode220. 存在重复元素 III | Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- [Swift]LeetCode451. 根据字符出现频率排序 | Sort Characters By Frequency
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
- ASP.NET Core 2.0 MVC项目实战
一.前言 毕业后入职现在的公司快有一个月了,公司主要的产品用的是C/S架构,再加上自己现在还在学习维护很老的delphi项目,还是有很多不情愿的.之前实习时主要是做.NET的B/S架构的项目,主要还是 ...
- 并发编程(十三)—— Java 线程池 实现原理与源码深度解析 之 Executors(三)
前两篇文章讲了线程池的源码分析,再来看这篇文章就比较简单了, 本文主要讲解 Executors 这个工具类,看看长江创建线程池的几种方法. newFixedThreadPool 生成一个固定大小的线程 ...
- .net core使用EasyNetQ做EventBus
随着SOA.微服务.CQRS的盛行,EventBus越来越流行,上GitHub搜了一下,还是有蛮多的这类实现,老牌的有NServiceBus(收费).MassTransit,最近的有CAP(国人写的, ...
- redis 系列22 复制Replication (下)
一. 复制环境准备 1.1 主库环境(172.168.18.201) 环境 说明 操作系统版本 CentOS 7.4.1708 IP地址 172.168.18.201 网关Gateway 172. ...
- Chapter 5 Blood Type——13
"Kryptonite doesn't bother me, either," he chuckled. “氪星石也不会影响我,” 他笑着说道. "You're not ...