一、绑定
1、Model
public class CItem {
private int ID;
private String Value = "";
public CItem() {
ID = 0;
Value = "";
}
public CItem(int _ID, String _Value) {
ID = _ID;
Value = _Value;
}
@Override
public String toString() {
// 为什么要重写toString()呢?因为适配器在显示数据的时候,如果传入适配器的对象不是字符串的情况下,直接就使用对象.toString()
// TODO Auto-generated method stub
return Value;
}
public int GetID() {
return ID;
}
public String GetValue() {
return Value;
}
}
2、绑定
List<CItem> lst = new ArrayList<CItem>();
for (int i = 0; i < 10; i++) {
/*
* 首先新建一个list,赋值
* ID为序号
*/
CItem item = new CItem(i, "value of " + i);
lst.add(item);
}
ArrayAdapter<CItem> myaAdapter = new ArrayAdapter<CItem>(this, android.R.layout.simple_spinner_item, lst);
mySpinner.setAdapter(myaAdapter);
3、 取值
mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
/*
* ids是刚刚新建的list里面的ID
*/
int ids = ((CItem) mySpinner.getSelectedItem()).GetID();
System.out.println(ids);
Toast.makeText(getApplicationContext(), String.valueOf(ids), Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});