SimpleAdapter初级使用
来源:互联网 发布:长安汽车软件下载 编辑:程序博客网 时间:2024/05/16 19:44
public class MainActivity extends Activity { private String[] name = { "张龙", "赵虎", "王朝", "马汉" }; private String[] desc = { "保镖一号", "保镖二号", "保镖三号", "保镖四号" }; private int[] images = {R.drawable.bubble1,R.drawable.bubble2,R.drawable.bubble3,R.drawable.bubble4}; private ListView lt1; private Map<String, Object> map; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); lt1 = (ListView)findViewById (R.id.List_View); ArrayList<Map<String,Object>> arrayList = new ArrayList<Map<String,Object>> (); for (int i=0;i<name.length;i++){ map = new HashMap<String,Object> (); map.put ("name",name[i]); map.put ("iamge",images[i]); map.put ("desc",desc[i]); arrayList.add (map); } SimpleAdapter simpleAdapter = new SimpleAdapter (MainActivity.this,arrayList,R.layout.simple,new String[]{"iamge","name","desc"},new int[]{R.id.IV,R.id.TV1,R.id.TV2}); lt1.setAdapter (simpleAdapter); }}
可以看到,几个要素
1、创建一个List,这样才能把数据展示到SimpleAdapter上。List匹配的类型是Map类型。也就是下面的Map。
2、new Map,不过Map是一个接口,所以用多态,Map<String,Object> map = new HashMap<String,Object>();key是String类型,value是Object类型。
这个Map是等会要放入List中的。
3、new一个SimpleAdapter,传入几个参数,context,list,布局,String数组,int数组。String数组里面的数据表示Map中的key,int数组中的数据表示String数组中的数据要对应显示的View控件。
如上面的范例,new String[]{"iamge","name","desc"},new int[]{R.id.IV,R.id.TV1,R.id.TV2},iamge对应的就是R.id.IV(ImageView控件),总之,就是要用合适的控件匹配相应的数据。
4、最后setAdapter即可。
5、List中的数据是Map,而Map中又包含了key和value,一个Map可以包含多个数据,直到被添加入List。因此在最后的呈现上,可以在一行item上呈现多个数据。
0 0
- SimpleAdapter初级使用
- SimpleAdapter使用
- SimpleAdapter 使用
- SimpleAdapter 使用
- SimpleAdapter使用
- SimpleAdapter适配器的使用
- 使用SimpleAdapter创建ListView
- Android ListView使用SimpleAdapter
- ListView-使用SimpleAdapter创建
- SimpleAdapter中ViewBinder 使用
- 使用SimpleAdapter填充ListView
- 使用SimpleAdapter创建ListView
- SimpleAdapter的基本使用
- ListView -- SimpleAdapter的使用
- SimpleAdapter详解使用
- simpleAdapter的使用
- simpleAdapter的使用
- SimpleAdapter的使用
- Android聊天列表Demo(QQ,微信,等通讯工具的聊天列表)
- 求解有向图的强连通分量的SCC问题---POJ 2186 Popular Cows
- ubuntu16.04-i386安装后
- jQuery选择器
- 优秀wordpress模板必备的五大条件
- SimpleAdapter初级使用
- C++ 关于构造,赋值函数
- 355. Design Twitter
- Swift编程基础之数组去重 + 模拟双色球
- Android studio使用HttpClient出错
- [译]接口隔离原则在Android中的实践
- JDBC核心
- 笔试题中的必胜策略
- Glut处理鼠标事件