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控件同样具有非常丰富的功能,在接下来的笔记中我们将一起 ...
随机推荐
- 判断二叉树是否BST
一.问题: 请实现一个函数,检查一棵二叉树是否为二叉查找树.给定树的根结点指针TreeNode* root,请返回一个bool,代表该树是否为二叉查找树. 二.思路: 解法一:从根节点开始遍历二叉树, ...
- [Swift]LeetCode282. 给表达式添加运算符 | Expression Add Operators
Given a string that contains only digits 0-9 and a target value, return all possibilities to add bin ...
- [Swift]LeetCode693. 交替位二进制数 | Binary Number with Alternating Bits
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...
- [Swift]LeetCode706. 设计哈希映射 | Design HashMap
Design a HashMap without using any built-in hash table libraries. To be specific, your design should ...
- [Swift]LeetCode830. 较大分组的位置 | Positions of Large Groups
In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...
- 在.net core上使用Entity FramWork(Db first)
在.net core中不可以向往常一样去直接可视化创建EF了,那我们可以通过命令安装 其依赖项有 Install-package Microsoft.EntityFrameworkCore Insta ...
- ELK快速搭建日志平台
1. 抛砖引入 <Elasticsearch> <Logstash> <Filebeat> <Filebeat模块与配置> <Kibana> ...
- 如何发起、防御和测试XSS攻击,我们用DVWA来学习(上)
XSS 全称Cross Site Scripting 即‘跨站脚本攻击’. 从其中文释义我们能直观的知道,这是一种对网站的攻击方式. 其原理在于,使用一切可能手段,将可执行脚本(scripting)植 ...
- 贝叶斯个性化排序(BPR)算法小结
在矩阵分解在协同过滤推荐算法中的应用中,我们讨论过像funkSVD之类的矩阵分解方法如何用于推荐.今天我们讲另一种在实际产品中用的比较多的推荐算法:贝叶斯个性化排序(Bayesian Personal ...
- Docker折腾手记-linux下安装
Linux下的安装方法 博主用的是centos7,其它也是大同小异 我根据的是官网的教程进行的操作,地址是 https://docs.docker.com/engine/installation/li ...