转载请注明出处!!!
simpleAdapter的构造函数 如下:
SimpleAdapter(Context context, List <? extends Map <String, ?>> data, int resource, String[] from, int[] to)
参数context上下文,一半用this,表示在该组件上显示
参数data,表示生成一个Map(String ,Object)列表选项,即要加载到ListView中的数据
参数resource listview布局文件
from和to表示用key-value形式存放数据,通过这个将数据连接起来
public class ListViewItem extends Activity {
private ListView shapeList;
private SimpleAdapter simpleAdapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.shapeList = (ListView)super.findViewById(R.id.shapelist);
simpleAdapter = new SimpleAdapter(this, getData(), R.layout.shape_list,
new String[]{"content", "image"},
new int[] {R.id.content, R.id.shape_img});
this.shapeList.setAdapter(this.simpleAdapter);//用simpleAdapter适配器填充listview
}
private List<Map<String, Object>> getData() {
String[] items = getResources().getStringArray(R.array.drawings);
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
//Map 以key-value 的形式存放 HashMap无序存放,key不允许重复
Map<String, Object> map = new HashMap<String, Object>();
map.put("content", items[0]);
map.put("image", R.drawable.draw);
list.add(map);
map = new HashMap<String, Object>();
map.put("content", items[1]);
map.put("image", R.drawable.straight_line);
list.add(map);
map = new HashMap<String, Object>();
map.put("content", items[2]);
map.put("image", R.drawable.rectangle);
list.add(map);
map = new HashMap<String, Object>();
map.put("content", items[3]);
map.put("image", R.drawable.ellipse);
list.add(map);
map = new HashMap<String, Object>();
map.put("content", items[4]);
map.put("image", R.drawable.circle);
list.add(map);
map = new HashMap<String, Object>();
map.put("content", items[5]);
map.put("image", R.drawable.points);
list.add(map);
map = new HashMap<String, Object>();
map.put("content", items[6]);
map.put("image", R.drawable.brokenline);
list.add(map);
map = new HashMap<String, Object>();
map.put("content", items[7]);
map.put("image", R.drawable.bezier);
list.add(map);
map = new HashMap<String, Object>();
map.put("content", items[8]);
map.put("image", R.drawable.triangle);
list.add(map);
return list;
}
}
主配置文件:shape_main.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:background="@drawable/background"
android:orientation="vertical" >
<ListView
android:id="@+id/shapelist"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
要显示内容的配置文件:shape_list.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/shape_img"
android:layout_width="40dp"
android:layout_height="50dp" />
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textColor="#FFFFFF"
android:textSize="20sp" />
</LinearLayout>
然后是实例演示效果