public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listview = (ListView) findViewById(R.id.listView1); // 獲取列表視圖
int[] imageId = new int[] { R.drawable.img01, R.drawable.img02,
R.drawable.img03, R.drawable.img04, R.drawable.img05,
R.drawable.img06, R.drawable.img07, R.drawable.img08 }; // 定義並初始化保存圖片id的數組
String[] title = new String[] { "保密設置", "安全", "系統設置", "上網", "個人文檔",
"GPS導航", "個人音樂", "E-mail" }; // 定義並初始化保存列表項文字的數組
List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>(); // 建立一個list集合
// 經過for循環將圖片id和列表項文字放到Map中,並添加到list集合中
for (int i = 0; i < imageId.length; i++) {
Map<String, Object> map = new HashMap<String, Object>(); // 實例化Map對象
map.put("image", imageId[i]);
map.put("title", title[i]);
listItems.add(map); // 將map對象添加到List集合中
} 數組
SimpleAdapter adapter = new SimpleAdapter(this, listItems,
R.layout.items, new String[] { "title", "image" }, new int[] {
R.id.title, R.id.image }); // 建立SimpleAdapter
listview.setAdapter(adapter); // 將適配器與ListView關聯 安全
}
} ide