继承ListActivity就可以不用setContentView,Android也会自动帮我们构造出一个全屏的列表。
public class SettingActivity extends ListActivity {
private static String[] allTest = new String[]{
"CPU测试",
"LCD测试",
"Battery测试",
"Backlight测试",
"PhoneCall测试",
"Vibrator测试",
"Flashlight测试",
"BT测试",
"Audio测试",
"Memory测试",
"Video测试",
"OpenGL测试",
};
private static int[] icon = new int[]{
R.drawable.cpu,
R.drawable.lcd,
R.drawable.connectivity,
R.drawable.battery,
R.drawable.call,
R.drawable.video,
R.drawable.fm,
R.drawable.bt,
R.drawable.audio,
R.drawable.memory,
R.drawable.pbook,
R.drawable.mmc
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
/**
* 一个map对应列表中的一项数据
*/
List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
//初始化列表项需要显示的内容
for (int i = 0; i < allTest.length; i++) {
Map<String, Object> item = new HashMap<String, Object>();
item.put("image", icon[i]);
item.put("textview", allTest[i]);
dataList.add(item);
}
// 构建适配器Adapter,将数据与显示数据的布局页面绑定
SimpleAdapter simpleAdapter = new SimpleAdapter(this, dataList,
R.layout.list_item, new String[] { "textview", "image" },
new int[] { R.id.text_name, R.id.image});
// 通过setAdapter()方法把适配器设置给ListView
setListAdapter(simpleAdapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
//列表项单击事件
super.onListItemClick(l, v, position, id);
}
}