Spinner一般绑定字符串数组就可以了,比如之前 的做法 ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.fruit, android.R.layout.simple_spinner_item);,有时候,我们需要绑定一个对象,比如显示的时候,显示value,但是程序需要获取的是key值,这个时候,我们就需要绑定自定义的对象。试验场景:证件类型下拉框,显示给用户看的是身份证、护照、户口本等等的文字,但是我们需要记录数据库的是对应的key 比如0,1,2。
一、设计Activity SpinnerKeyValueActivity
/**
*
*/
package com.figo.helloworld;
import java.util.ArrayList;
import java.util.List;
import android.view.View;
import android.app.Activity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
/**
* @author zhuzhifei
*
*/
public class SpinnerKeyValueActivity extends Activity {
private Spinner spcerticatetype;
private TextView tvcerticatetype;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//设置页面
setContentView(R.layout.spinnerkeyvalue);
spcerticatetype=(Spinner)findViewById(R.id.spcerticatetype);
tvcerticatetype=(TextView)findViewById(R.id.tvcerticatetype);
//绑定数据到spinner
BindData();
}
//绑定数据到spinner
private void BindData()
{
// 设置证件类型
List<SpinnerItem> lst = new ArrayList<SpinnerItem>();
SpinnerItem item0 = new SpinnerItem("0", "身份证");
SpinnerItem item1 = new SpinnerItem("1", "护照");
SpinnerItem item2 = new SpinnerItem("2", "军官证");
SpinnerItem item3 = new SpinnerItem("3", "士兵证");
SpinnerItem item4 = new SpinnerItem("4", "回乡证");
SpinnerItem item5 = new SpinnerItem("5", "临时身份证");
SpinnerItem item6 = new SpinnerItem("6", "户口本");
SpinnerItem item7 = new SpinnerItem("7", "其他");
SpinnerItem item8 = new SpinnerItem("8", "外国人居留证");
SpinnerItem item9 = new SpinnerItem("9", "警官证");
lst.add(item0);
lst.add(item1);
lst.add(item2);
lst.add(item3);
lst.add(item4);
lst.add(item5);
lst.add(item6);
lst.add(item7);
lst.add(item8);
lst.add(item9);
ArrayAdapter<SpinnerItem> aradapter = new ArrayAdapter<SpinnerItem>(
this.getApplicationContext(),
android.R.layout.simple_spinner_item, lst);
aradapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spcerticatetype.setAdapter(aradapter);
//选中某个选项后,添加事件
spcerticatetype.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(
AdapterView<?> parent, View view, int position, long id) {
SpinnerItem item = (SpinnerItem) spcerticatetype.getSelectedItem();
tvcerticatetype.setText("您选择的是:key="+item.Key+",value="+item.Value);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
// 定义下拉框选项
public class SpinnerItem {
private String Key = "";
private String Value = "";
public SpinnerItem() {
Key = "";
Value = "";
}
public SpinnerItem(String _Key, String _Value) {
Key = _Key;
Value = _Value;
}
@Override
public String toString() {
// 为什么要重写toString()呢?因为适配器在显示数据的时候,如果传入适配器的对象不是字符串的情况下,直接就使用对象.toString()
// TODO Auto-generated method stub
return Value;
}
public String GetKey() {
return Key;
}
public String GetValue() {
return Value;
}
}
}
二、运行效果
this, R.array.fruit, android.R.layout.simple_spinner_item);,有时候,我们需要绑定一个对象,比如显示的时候,显示value,但是程序需要获取的是key值,这个时候,我们就需要绑定自定义的对象。试验场景:证件类型下拉框,显示给用户看的是身份证、护照、户口本等等的文字,但是我们需要记录数据库的是对应的key 比如0,1,2。
一、设计Activity SpinnerKeyValueActivity
/**
*
*/
package com.figo.helloworld;
import java.util.ArrayList;
import java.util.List;
import android.view.View;
import android.app.Activity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
/**
* @author zhuzhifei
*
*/
public class SpinnerKeyValueActivity extends Activity {
private Spinner spcerticatetype;
private TextView tvcerticatetype;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//设置页面
setContentView(R.layout.spinnerkeyvalue);
spcerticatetype=(Spinner)findViewById(R.id.spcerticatetype);
tvcerticatetype=(TextView)findViewById(R.id.tvcerticatetype);
//绑定数据到spinner
BindData();
}
//绑定数据到spinner
private void BindData()
{
// 设置证件类型
List<SpinnerItem> lst = new ArrayList<SpinnerItem>();
SpinnerItem item0 = new SpinnerItem("0", "身份证");
SpinnerItem item1 = new SpinnerItem("1", "护照");
SpinnerItem item2 = new SpinnerItem("2", "军官证");
SpinnerItem item3 = new SpinnerItem("3", "士兵证");
SpinnerItem item4 = new SpinnerItem("4", "回乡证");
SpinnerItem item5 = new SpinnerItem("5", "临时身份证");
SpinnerItem item6 = new SpinnerItem("6", "户口本");
SpinnerItem item7 = new SpinnerItem("7", "其他");
SpinnerItem item8 = new SpinnerItem("8", "外国人居留证");
SpinnerItem item9 = new SpinnerItem("9", "警官证");
lst.add(item0);
lst.add(item1);
lst.add(item2);
lst.add(item3);
lst.add(item4);
lst.add(item5);
lst.add(item6);
lst.add(item7);
lst.add(item8);
lst.add(item9);
ArrayAdapter<SpinnerItem> aradapter = new ArrayAdapter<SpinnerItem>(
this.getApplicationContext(),
android.R.layout.simple_spinner_item, lst);
aradapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spcerticatetype.setAdapter(aradapter);
//选中某个选项后,添加事件
spcerticatetype.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(
AdapterView<?> parent, View view, int position, long id) {
SpinnerItem item = (SpinnerItem) spcerticatetype.getSelectedItem();
tvcerticatetype.setText("您选择的是:key="+item.Key+",value="+item.Value);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
// 定义下拉框选项
public class SpinnerItem {
private String Key = "";
private String Value = "";
public SpinnerItem() {
Key = "";
Value = "";
}
public SpinnerItem(String _Key, String _Value) {
Key = _Key;
Value = _Value;
}
@Override
public String toString() {
// 为什么要重写toString()呢?因为适配器在显示数据的时候,如果传入适配器的对象不是字符串的情况下,直接就使用对象.toString()
// TODO Auto-generated method stub
return Value;
}
public String GetKey() {
return Key;
}
public String GetValue() {
return Value;
}
}
}
二、运行效果