Android spinner取Value和Text的值
先声明一个对象,并重写对象的toString()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class SpinnerItem {
private String ID = "";
private String Value = "";
public SpinnerItem () {
ID = "";
Value = "";
}
public SpinnerItem (String _ID, String _Value) {
ID = _ID;
Value = _Value;
}
@Override
public String toString() { //为什么要重写toString()呢?因为适配器在显示数据的时候,如果传入适配器的对象不是字符串的情况下,直接就使用对象.toString()
// TODO Auto-generated method stub
return Value;
}
public String GetID() {
return ID;
}
public String GetValue() {
return Value;
}
}
然后声明对象实例并加入到arraylist里面,并设置spinner的适配器
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
Spinner Sp = (Spinner).............//
List<CItem > lst = new ArrayList<CItem>();
CItem ct = new CItem ("1","测试");
lst.Add(ct);;
ArrayAdapter<CItem > Adapter = new ArrayAdapter<CItem>(context,
android.R.layout.simple_spinner_item, lst);
Sp.SetAdapter(Adapter);
取值:
如果取TEXT值则可以直接取:Sp.getSelectedItem.ToString()或者:((CItem)Sp.getSelectedItem). GetValue() ;
如果去Value值则可以这样取:((CItem)Sp.getSelectedItem).GetID();