public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)Since: API Level 1ConstructorParameterscontext 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);
}
}
本文详细介绍了如何利用SimpleAdapter在Android中实现列表视图的自定义适配,包括如何创建适配器、设置布局资源、处理数据集以及绑定视图到数据元素。
327

被折叠的 条评论
为什么被折叠?



