1、视图
1)主视图
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" > <ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv"
></ListView> </RelativeLayout>
2)item视图
<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv"
/> </LinearLayout>
2、java代码
package com.example.simpleadapter; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;
import android.widget.SimpleAdapter; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); ListView lv = (ListView)findViewById(R.id.lv); List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
Map<String,Object> map1 = new HashMap<String,Object>();
map1.put("nametext", "我是第一个功能");
map1.put("iconid", R.drawable.btn_minus_disable_focused); Map<String,Object> map2 = new HashMap<String,Object>();
map2.put("nametext", "我是第二个功能");
map2.put("iconid", R.drawable.btn_minus_selected); Map<String,Object> map3 = new HashMap<String,Object>();
map3.put("nametext", "我是第三个功能");
map3.put("iconid", R.drawable.btn_radio_off_selected); Map<String,Object> map4 = new HashMap<String,Object>();
map4.put("nametext", "我是第四个功能");
map4.put("iconid", R.drawable.btn_radio_on_focused_holo_dark); Map<String,Object> map5 = new HashMap<String,Object>();
map5.put("nametext", "我是第五个功能");
map5.put("iconid", R.drawable.btn_radio_on_holo_dark); data.add(map1);
data.add(map2);
data.add(map3);
data.add(map4);
data.add(map5); lv.setAdapter(new SimpleAdapter(this, data, R.layout.list_item, new String[]{"nametext","iconid"}, new int[]{R.id.tv,R.id.iv}));
} }