SimpleAdapter:

SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

参数:

1.context:上下文

2.data:Map<String, object>列表,列表要显示的数据,Map列表中的key要与参数”from“中的内容保持一致

3.resource:item的布局文件,这个布局中必须包括参数”to“中定义的控件id

4.from:表示该Map对象的key对应value来生成列表项

5.to:表示来填充的组件 Map对象key对应的资源一依次填充组件 顺序有对应关系

ListView的SimpleAdapter的使用

代码:

  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5.  
  6. import android.app.Activity;
  7. import android.os.Bundle;
  8. import android.widget.ListView;
  9. import android.widget.SimpleAdapter;
  10.  
  11. public class MainActivity extends Activity {
  12.  
  13. private ListView lv;
  14.  
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_main);
  19.  
  20. List<Map<String, Object>> data = new ArrayList<>();
  21.  
  22. Map<String, Object> map1 = new HashMap<>();
  23. map1.put("photo", R.drawable.img01);
  24. map1.put("name", "小志");
  25. data.add(map1);
  26.  
  27. Map<String, Object> map2 = new HashMap<>();
  28. map2.put("photo", R.drawable.img02);
  29. map2.put("name", "小志的儿子");
  30. data.add(map2);
  31.  
  32. Map<String, Object> map3 = new HashMap<>();
  33. map3.put("photo", R.drawable.img03);
  34. map3.put("name", "小志的老婆");
  35. data.add(map3);
  36.  
  37. Map<String, Object> map4 = new HashMap<>();
  38. map4.put("photo", R.drawable.img04);
  39. map4.put("name", "萌萌");
  40. data.add(map4);
  41.  
  42. lv = (ListView) findViewById(R.id.lv);
  43.  
  44. lv.setAdapter(new SimpleAdapter(this, data, R.layout.item_view,
  45. new String[] { "photo", "name" }, new int[] { R.id.lv_phono,
  46. R.id.lv_name }));
  47.  
  48. }
  49.  
  50. }

item_view布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:orientation="horizontal" >
  6.  
  7. <ImageView
  8. android:id="@+id/lv_phono"
  9. android:layout_width="60dp"
  10. android:layout_height="60dp"
  11. android:src="@drawable/img01" />
  12.  
  13. <TextView
  14. android:id="@+id/lv_name"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:layout_gravity="center_vertical"
  18. android:text="名字"
  19. android:textSize="20sp" />
  20.  
  21. </LinearLayout>
  1. activity_main布局文件:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=".MainActivity" >
  6.  
  7. <ListView
  8. android:id="@+id/lv"
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent" >
  11. </ListView>
  12.  
  13. </RelativeLayout>

Android开发之ListView-SimpleAdapter的使用的更多相关文章

  1. 【转】Android开发之ListView+EditText-要命的焦点和软键盘问题解决办法

    Android开发之ListView+EditText-要命的焦点和软键盘问题解决办法 [原文链接] 这篇文章完美的解决了我几个月没结论的bug... 感谢热爱分享的技术达人~ 我是怎么走进这个大坑的 ...

  2. android 开发之 ListView 与Adapter 应用实践

    在开发android中,ListView 的应用显得非常频繁,只要需要显示列表展示的应用,可以说是必不可少,下面是记录开发中应用到ListView与Adapter 使用的实例: ListView 所在 ...

  3. Android开发之ListView添加多种布局效果演示

    在这个案例中展示的新闻列表,使用到ListView控件,然后在适配器中添加多种布局效果,这里通过重写BaseAdapter类中的 getViewType()和getItemViewType()来做判断 ...

  4. Android开发之ListView实现不同品种分类分隔栏的效果(非ExpandableListView实现)

    我们有时候会遇到这么一个情况.就是我在一个ListView里面须要显示的东西事实上是有种类之分的.比方我要分冬天,夏天.秋天.春天,然后在这每一个季节以下再去载入各自的条目数据. 还有,比方我们的通讯 ...

  5. Android开发之ListView详解 以及简单的listView优化

    ListView列表视图 最常用的控件之一,使用场景例如:微信,手机QQ等等. android:divider:每个item之间的分割线,可以使用图片或者色值. android:dividerHeig ...

  6. Android开发之ListView设置隔行变色

    public class HLCheckAdapter extends BaseAdapter { private List<HuoLiang> list; private Context ...

  7. Android开发之ListView条目批量选择删除

    ListView实现的列表,假设是可编辑,可删除的,一般都要提供批量删除功能,否则的话,一项一项的删除体验非常不好,也给用户带来了非常大的麻烦. 实现效果图 详细实现代码 select.xml 主布局 ...

  8. android开发之 listview中的item去掉分割线 隐藏分割线

    有三种方法: 1> 设置android:divider="@null" 2> android:divider="#00000000" #000000 ...

  9. 【Android UI】Android开发之View的几种布局方式及实践

    引言 通过前面两篇: Android 开发之旅:又见Hello World! Android 开发之旅:深入分析布局文件&又是“Hello World!” 我们对Android应用程序运行原理 ...

  10. Android开发之Java集合类性能分析

    对于Android开发者来说深入了解Java的集合类很有必要主要是从Collection和Map接口衍生出来的,目前主要提供了List.Set和 Map这三大类的集合,今天Android吧(ard8. ...

随机推荐

  1. 斐波那契数 c 语言实现

    斐波那契数列,又称黄金数列,指的是这样一个数列:1.1.2.3.5.8.13.21.……在数学上,斐波纳契数列以如下被以递归的方法定义:F(1)=1,F(2)=1,F(n)=F(n-1)+F(n-2) ...

  2. DEV GridControl表格数据源为空在表格中间显示提醒字符

    private static void gv_CustomDrawEmptyForeground(object sender, DevExpress.XtraGrid.Views.Base.Custo ...

  3. 连续改变Chrome浏览器窗口大小,可以导致内存泄漏

    最近在做响应式布局的页面,在开发测试过程中,为了看到页面在不同尺寸的窗口中的表现,因此要不停的拖动浏览器来改变其窗口大小:开始在Chrome浏览器下查看页面,拖动了几次,感觉电脑明显的卡了下来,刚开没 ...

  4. php提取字符串中的数字

    最近工作中写代码的时候需要在一串字符串中将所有的数字提取出来这么一个小功能,研究了一下发现方法还挺多,值得记录一下,于是对如何使用PHP将字符串中的数字提取出来的功能做了一个小总结,总结三种方法如下: ...

  5. 解决pxe网络批量安装部署linux遇到的问题和解决方法

    解决“出现Unable to retrieve 192.168.0.100/var/www/html/images/install.img错误” 分析:我们必须了解这个错误出现在哪个阶段才能正确找到错 ...

  6. DB2分区表删除和添加分区

    1.数据库版本 2.具体procedure DROP PROCEDURE DB2USER.TOOLS_PARTITION_TABLE_SHOW (VARCHAR ()); )) /********** ...

  7. Linux网络通信编程(套接字模型TCP\UDP与IO多路复用模型select\poll\epoll)

    Linux下测试代码: http://www.linuxhowtos.org/C_C++/socket.htm TCP模型 //TCPClient.c #include<string.h> ...

  8. table 中实现 控制 指定列的 左对齐 右对齐方式

    .listTable{ border-collapse:collapse; border-top:1px solid #8c9594; border-right:1px solid #8c9594; ...

  9. java中ReentrantReadWriteLock读写锁的使用

    Lock比传统线程模型中的synchronized方式更加面向对象,与生活中的锁类似,锁本身也应该是一个对象.两个线程执行的代码片段要实现同步互斥的效果,它们必须用同一个Lock对象. 读写锁:分为读 ...

  10. 最简单去Button回车事件

    描述: 有的时候,回车时,不想触发 页面上的保存按钮的事件. 有一种最简单的解决 方法: 一,把form增加一个不用的默认button <form id="form1" ru ...