ListView是开发过程中用得最多的列表组件。
package com.example.phonedemo;
import com.example.phonedemo.util.Utils;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
public class ArrayAdapterDemo extends Activity {
private LinearLayout layout = null;
private ListView list = null;
private ArrayAdapter adapter = null;
private String[] arr = { "Beijing", "Shanghai", "Guangzhou", "Shenzhen",
"Guiyang", "Kunming", "Nanjing", "Hefei" };
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.layout = new LinearLayout(this);
this.layout.setOrientation(LinearLayout.VERTICAL);
this.list = new ListView(this);
this.adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, arr);
this.list.setAdapter(adapter);
this.list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(ArrayAdapterDemo.this,
ArrayAdapterDemo.this.arr[arg2], Toast.LENGTH_SHORT)
.show();
}
});
this.layout.addView(list, Utils.match);
super.addContentView(this.layout, Utils.match);
}
}