//設置Spinner樣式android.R.layout.simple_spinner_item,綁定數組數據array,將數組內容和ArrayAdapter連接
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, array);
adapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(adapter);
//設置默認選中
mSpinner.setSelection(2,true);
//添加監聽事件
mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {
//獲取選中值
Spinner spinner = (Spinner) adapterView;
String data = (String) spinner.getItemAtPosition(position);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
參考:http://www.cnblogs.com/over140/archive/2010/09/25/1834469.html
二. Spinner的數據綁定方式
1、 通過數組字符串綁定
private static final String[] m_Countries = { "111", "222", "333", "444", "555" }; //定義數組
public String[] petArray = new String[] { "狗", "貓", "其他" };
使用方法如上例
2、通過資源文件配置:比如values文件夾中的:arrays.xml、colors.xml、ids.xml、strings.xml、styles.xml等等
<string-array name="debitCardNumber">
<item>信用卡111</item>
<item>信用卡222</item>
<item>信用卡333</item>
<item>信用卡444</item>
<item>信用卡555</item>
</string-array>
綁定方法:
// 准備一個數組適配器
// R.array.colors : 直接從strings.xml去數據
// android.R.layout.simple_spinner_item : 設置Spinner樣式(無下拉列表時)
ArrayAdapter<CharSequence> arrayAdapter = ArrayAdapter
.createFromResource(this, R.array.colors,
android.R.layout.simple_spinner_item);
// 設置下拉列表樣式
arrayAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// 為下拉列表設置適配器
spinner.setAdapter(arrayAdapter);
// 為下拉列表綁定事件監聽器
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(
SpinnerDemoActivity.this,
"選擇的色彩: "
+ parent.getItemAtPosition(position).toString(),
Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
3、數據庫存儲
三、 Spinner實現省市二級聯動
ArrayAdapter<String> adapterProvince = new ArrayAdapter<String>(
this,
android.R.layout.simple_spinner_item, provinceArray);
adapterProvince
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinnerProvince.setAdapter(adapterProvince);
// mSpinnerProvince.setSelection(2,true);//設置默認選中
mSpinnerProvince
.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView,
View view, int position, long id) {
Spinner spinner = (Spinner) adapterView;
String pro = (String) spinner
.getItemAtPosition(position);
mUserProvince = pro;
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_spinner_item, city);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinnerCity.setAdapter(adapter);
mSpinnerCity
.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(
AdapterView<?> adapterView,
View view, int position, long id) {
Spinner spinner = (Spinner) adapterView;
String pro = (String) spinner
.getItemAtPosition(position);
mUserCity = pro;
Log.v("city", "mUserProvince:"
+ mUserProvince
+ " mUserCity:" + mUserCity);
}
@Override
public void onNothingSelected(
AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
直接用string設置默認選中的方法:
int pos=adapter.getPosition(str); mSpinnerCity.setSelection(pos);
