[android]代码库
myListView = new ListView(this);
LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
myListView.setBackgroundColor(getResources()
.getColor(R.drawable.ltgray));
/* 将ListView加到myLinearLayout */
myLinearLayout.addView(myListView, param2);
/* 将LinearLayout加到ContentView */
setContentView(myLinearLayout);
/* new ArrayAdapter对象并将array字符串数组传入 */
ArrayAdapter adapter = new ArrayAdapter(this,
R.layout.my_simple_list_item, array);
/* 将ArrayAdapter加入ListView对象中 */
myListView.setAdapter(adapter);
/* myListView加入OnItemSelectedListener */
myListView
.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView arg0, View arg1,
int arg2, long arg3) {
/* 使用getSelectedItem()将选取的值带入myTextView中 */
myTextView.setText("你选的是"
+ arg0.getSelectedItem().toString());
}
@Override
public void onNothingSelected(AdapterView arg0) {
// TODO Auto-generated method stub
}
});
/* myListView加入OnItemClickListener */
myListView
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView arg0, View arg1,
int arg2, long arg3) {
/*
* 使用String[index],arg2为点选到ListView的index,并将值带入myTextView中
*/
myTextView.setText("你选的是" + array[arg2]);
}
});