public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
Since: API Level 1
Constructor
Parameters
context The context where the View associated with this SimpleAdapter is running
data A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"
resource Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to"
from A list of column names that will be added to the Map associated with each item.
to The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.
package cn.jxufe.iet.android;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.jxufe.iet.android.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class SimpleAdapterTest extends Activity{
private String[] names = new String[]{ "虎头", "弄玉", "李清照", "李白"};
private int[] imageIds = new int[]{ R.drawable.tiger , R.drawable.nongyu
, R.drawable.qingzhao , R.drawable.libai};
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建一个List集合,List集合的元素是Map
List<Map<String, Object>> listItems = new ArrayList
<Map<String, Object>>();
for (int i = 0; i < names.length; i++){
Map<String, Object> listItem = new HashMap
<String, Object>();
listItem.put("header", imageIds[i]);
listItem.put("personName", names[i]);
listItems.add(listItem);
}
//创建一个SimpleAdapter
SimpleAdapter simpleAdapter = new SimpleAdapter(this
, listItems
, R.layout.main
, new String[]{ "personName", "header" }
, new int[]{R.id.name , R.id.header});
ListView list = (ListView)findViewById(R.id.mylist);
//为ListView设置Adapter
list.setAdapter(simpleAdapter);
}
}