Android UI设计——ListView控件与SimpleAdapter适配器(三)
来源:互联网 发布:建筑图酷软件 编辑:程序博客网 时间:2024/05/06 10:28
SimpleAdapter
SimpleAdapter也是Android自己提供的一个Adapter适配器,它与ArrayAdapter不同的是ArrayAdapter需要使用Android自己定义的view布局文件,而SimpleAdapter则可以使用我们自己定义的布局文件。要学习SimpleAdapter的使用首先然我们看一下SimpleAdapter的构造器:
从图片中我们可以看出,SimpleAdapter只有一个构造器:
- 第一个参数
Context context
是指当前的Activity,我们传入this即可。 - 第二个参数
List<? extends Map<String, ?>>
是指传入的数据类型必须是List集合,集合存放的数据类型必须是Map。 - 第三个参数
int resource
是指View的布局文件。也就是用来显示数据的View。 - 第四个参数
String[] from
数据是以Map类型存放在List集合中的,from参数是指存放在List中每条Map数据的键值集合。 第五个参数
int[] to
是指将每条Map类型的数据中的不同键值对应到不同的得布局控件中。介绍完构造器的参数,大家可能还是不太懂到底是应该如何使用,没关系,我们通过例子来说明使用。
SimpleAdapter的使用
One.定义一个ListView的布局文件。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/listview_array" android:layout_width="match_parent" android:layout_height="wrap_content" android:cacheColorHint="#00000000" android:divider="#f00000" android:dividerHeight="2dp"> </ListView></LinearLayout>
Two.书写一个View的布局文件,将数据以该View的形式存放在ListView中。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="20dp" android:gravity="center|left" android:background="@drawable/item_background"> <ImageView android:id="@+id/image_photo" android:layout_width="70dp" android:layout_height="70dp" /> <TextView android:id="@+id/textview_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="姓名" android:textStyle="bold" android:textColor="#0e99ff" android:textSize="20sp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginRight="15dp" android:layout_marginLeft="15dp"> <TextView android:id="@+id/textview_age" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="性别" android:textStyle="bold" android:textColor="#009900" android:textSize="15sp"/> <TextView android:id="@+id/textview_sex" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="年龄" android:textStyle="bold" android:textColor="#ff99ff" android:textSize="15sp" /> </LinearLayout> <TextView android:id="@+id/textview_hobby" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="爱好" android:textStyle="bold" android:textColor="#55eedd" android:textSize="20sp" /></LinearLayout>
Three.创建数据。创建List的集合存放Map类型的数据,并对其进行初始化。(这些直接在Activity中定义)
public HashMap<String, String> createHashMap(String name, String age, String sex, String hobby){ HashMap<String, String> map = new HashMap<String, String>(); map.put("name", name); map.put("age", age); map.put("sex",sex); map.put("hobby", hobby); return map; } public void initList(){ mData = new ArrayList<HashMap<String, String>>(); HashMap zhangsan = createHashMap("张三", "18", "男", "喜欢打篮球,睡觉"); mData.add(zhangsan); HashMap lisi = createHashMap("李四", "19", "女", "喜欢看书"); mData.add(lisi); HashMap wangwu = createHashMap("王五", "22", "男", "喜欢玩游戏"); mData.add(wangwu); HashMap zhaoliu = createHashMap("赵六", "21", "男", "喜欢吃饭,睡觉"); mData.add(zhaoliu); }
Four.在onCreate()方法中初始化数据,创建SimpleAdapter的对象
String[] from = new String[]{"name", "age", "sex", "hobby"}; int[] to = new int[]{R.id.textview_name,R.id.textview_age,R.id.textview_sex,R.id.textview_hobby}; initList(); SimpleAdapter simpleAdapter = new SimpleAdapter(this,mData,R.layout.item_simpleadapter,from,to);
Five.调用setAdapter()方法,将View添加到ListView中。
mListViewArray.setAdapter(simpleAdapter);
Activity整体的代码为:
public class ListActivity extends Activity { private ListView mListViewArray; //定义数据。 private List<HashMap<String, String >> mData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list); //创建ListView的对象。 mListViewArray = (ListView) findViewById(R.id.listview_array); //初始化数据,通过调用自己定义的初始化方法。 initList(); //创建SimpleAdapter对象,将添加到数据添加到自己定义的View中。 SimpleAdapter simpleAdapter = new SimpleAdapter(this,mData,R.layout.item_simpleadapter, new String[]{"name", "age", "sex", "hobby"},new int[]{R.id.textview_name,R.id.textview_age,R.id.textview_sex,R.id.textview_hobby}); //将View添加到ListView中。 mListViewArray.setAdapter(simpleAdapter); } //定义Map类型的数据。 public HashMap<String, String> createHashMap(String name, String age, String sex, String hobby){ HashMap<String, String> map = new HashMap<String, String>(); map.put("name", name); map.put("age", age); map.put("sex",sex); map.put("hobby", hobby); return map; } //对数据进行初始化的方法。 public void initList(){ mData = new ArrayList<HashMap<String, String>>(); HashMap zhangsan = createHashMap("张三", "18", "男", "喜欢打篮球,睡觉"); mData.add(zhangsan); HashMap lisi = createHashMap("李四", "19", "女", "喜欢看书"); mData.add(lisi); HashMap wangwu = createHashMap("王五", "22", "男", "喜欢玩游戏"); mData.add(wangwu); HashMap zhaoliu = createHashMap("赵六", "21", "男", "喜欢吃饭,睡觉"); mData.add(zhaoliu); }}
结果如下:
0 0
- Android UI设计——ListView控件与SimpleAdapter适配器(三)
- Android UI 设计:ListView 控件与 SimpleAdapter 适配器
- Android UI设计:ListView适配器——ArrayAdapter、SimpleAdapter与BaseAdapter
- Android UI设计——ListView控件与Adapter适配器(一)
- Android UI设计:ListView 控件与 Adapter 适配器
- Android UI设计——ListView控件和ArrayAdapter适配器(二)
- Android UI 设计:ListView控件和ArrayAdapter适配器(二)
- ListView控件:(2)SimpleAdapter适配器生成
- 【Android】ListView与SimpleAdapter适配器的使用
- Android UI基础——ListView控件及Adapter适配器
- Android 之 ListView列表控件 的SimpleAdapter适配器
- Android控件之ListView与SimpleAdapter
- Android学习笔记之——ListView 的两种适配器ArrayAdapter和SimpleAdapter
- 【Android学习笔记】ListView使用SimpleAdapter适配器
- android ListView适配器之SimpleAdapter的用法
- 【Android学习笔记】ListView使用SimpleAdapter适配器
- Android——ListView布局+适配器(三)
- Android UI设计——ListView控件和自定义Adapter(四)
- at91sam9g25处理器uboot移植 个人笔记一
- SMTP:防止追踪发件人IP
- http 返回码 405 解决方案之一
- win32(四)
- SQL Server 2008 / 2008 R2 定期自动备份数据库
- Android UI设计——ListView控件与SimpleAdapter适配器(三)
- C语言动态内存分配函数解析
- ListView和Adapter(适配器)
- SQL 行列转化
- LintCode Copy Books
- HDOJ-2016 数据交换输出
- Android OkHttp完全解析 是时候来了解OkHttp了
- Java解析优酷视频地址
- win32(五)