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); android
final ListView listView=(ListView)findViewById(R.id.listView1);
listView.addHeaderView(line()); //設置header view ide
/****************建立用於爲ListView指定列表項的適配器********************/
// 方法一
// String[] ctype=new String[]{"情景模式","主題模式","手機","程序管理"};
// ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_activated_1,ctype);
// 方法二
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.ctype,android.R.layout.simple_list_item_checked); //建立一個適配器
/***************************************************************************/
listView.setAdapter(adapter); // 將適配器與ListView關聯
listView.addFooterView(line()); //設置footer view
listView.setOnItemClickListener(new OnItemClickListener() { this
@Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos,
long id) {
String result = parent.getItemAtPosition(pos).toString(); // 獲取選擇項的值
Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
} xml
});
} 圖片
private View line() {
ImageView image=new ImageView(this); //建立一個圖像視圖
image.setImageResource(R.drawable.line1); //設置要顯示的圖片
return image;
}
} utf-8