通过ArrayAdapter实现Adapter虽然简单、易用,但ArrayAdapter的功能比较有限,它的每个列表项只能给一个TextView动态填充内容。如果开发者需要实现更复杂的列表项,则可以考虑使用 SimpleAdapter。
一、使用SimpleAdapter
这里需要注意的是,不要被SimpleAdapter的名字迷惑欺骗了,SimpleAdapter的功能不仅不简单,还十分强大,列表组件的大部分使用都是通过SimpleAdapter来提供列表项的。
在使用SimpleAdapter之前,先来一起学习SimpleAdapter的构造方法,其构造方法如下:
SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
从SimpleAdapter的构造方法可以看到,一共需要5个参数,这也是很多开发者觉得使用SimpleAdapter比较难的原因,其实就是没有很好的理解这5个参数。这个5个参数的含义如下:
二、示例
接下来通过一个示例程序来学习如何使用SimpleAdapter创建ListView。
继续使用WidgetSample工程的listviewsample模块,在app/main/res/layout/目录下创建simpleadapter_layout.xml文件,在其中填充如下代码片段:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
在res/layout/目录下新建一个simpleadapter_item.xml的列表项布局文件,其代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/icon_img"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp" />
<TextView
android:id="@+id/info_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
接下来为ListView提供Adapter,使用SimpleAdapter决定ListView所要显示的列表项。创建SimpleAdapterActivity.java文件,加载上面新建的布局文件,具体代码如下:
package com.jinyu.cqkxzsxy.android.listviewsample;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SimpleAdapterActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simpleadapter_layout);
// 获取界面组件
ListView listView = (ListView) findViewById(R.id.listview);
// 创建一个SimpleAdapter
SimpleAdapter adapter = new SimpleAdapter(this,
getData(),
R.layout.simpleadapter_item,
new String[]{"img", "title", "info"},
new int[]{R.id.icon_img, R.id.title_tv, R.id.info_tv});
// 为ListView设置Adapter
listView.setAdapter(adapter);
}
/**
* 创建一个List集合,其元素为Map
* @return 返回列表项的集合对象
*/
private List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("img", R.drawable.item_01);
map.put("title", "小宗");
map.put("info", "电台DJ");
list.add(map);
map = new HashMap<String, Object>();
map.put("img", R.drawable.item_02);
map.put("title", "貂蝉");
map.put("info", "四大美女");
list.add(map);
map = new HashMap<String, Object>();
map.put("img", R.drawable.item_03);
map.put("title", "奶茶");
map.put("info", "清纯妹妹");
list.add(map);
map = new HashMap<String, Object>();
map.put("img", R.drawable.item_04);
map.put("title", "大黄");
map.put("info", "是小狗");
list.add(map);
map = new HashMap<String, Object>();
map.put("img", R.drawable.item_05);
map.put("title", "hello");
map.put("info", "every thing");
list.add(map);
map = new HashMap<String, Object>();
map.put("img", R.drawable.item_06);
map.put("title", "world");
map.put("info", "hello world");
list.add(map);
return list;
}
}
上面的程序关键在于粗体字代码,其创建了一个SimpleAdapter。getData()方法生成一个长度为6的集合,意味着生成的ListView将会包含6个列表项,每个列表项都是R.layout.list_item对应的组件。
第一个列表项的数据是{“img”=R.id.item_01, “title”=“小宗”, “info”=“电台DJ”}Map集合。创建SimpleAdapter时第5个参数、第4个参数指定使用ID为R.id.icon_img组件显示img对应的值,使用ID为R.id.title_tv组件显示title对应的值,使用ID为R.id.info_tv组件显示info对应的值,这样第一个列表项组件所包含的三个组件都有了显示的内容。后面的列表项以此类推。
运行程序,可以看到下图所示界面效果。
SimpleAdapter 同样可作为 ListActivity 的内容Adapter,这样可以让用户方便地定制ListActivity所显示的列表项。
同ArrayAdapter创建ListView一样,如果需要监听用户单击、选中某个列表项的事件,则可以通过AdapterView的setOnltemClickListener()方法为单击事件添加监听器,或者通过 setOnItemSelectedListener()方法为列表项的选中事件添加监听器。