下拉菜单 Spinner 简单纯字符串版
public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener{
private Spinner spinner;
private TextView text;
private List<String> data = new ArrayList<String>();
private ArrayAdapter<String> adapter;
private String[] place = {"北京","上海","广东","苏州"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//注册控件
spinner = (Spinner)findViewById(R.id.spinner);
text = (TextView)findViewById(R.id.text);
//数据源
for(int i = 0; i < place.length ; i++){
data.add(place[i]);
}
//适配器
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,data);
adapter.setDropDownViewResource(R.layout.buju);
//与适配器绑定
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//方法1:通过ArrayAdapter 提供的getItem(position) 直接获取位置内容
String cityname = adapter.getItem(position);
text.setText("您选择的地方是:" + cityname);
//方法2:在一开始初始化数据源 时 用数组来定义 通过数组下标配合position来找
// text.setText("您选择的地方是:" + place[position]);
//方法3:通过list提供的 list.get(position) 直接获取位置内容
// String cityname = data.get(position);
// text.setText("您选择的地方是:" + cityname);
}