首页 > 代码库 > Spinner数据绑定方法
Spinner数据绑定方法
public Spinner mySpinner; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mySpinner = (Spinner) findViewById(R.id.Spinner_mySpinner); List<CItem> lst = new ArrayList<CItem>(); for (int i = 0; i < 10; i++) { /* * 首先新建一个list,赋值 ID为序号 */ CItem item = new CItem(i, "烟坡里隧道 " + i); lst.add(item); } ArrayAdapter<CItem> myaAdapter = new ArrayAdapter<CItem>(this, android.R.layout.simple_spinner_item, lst); mySpinner.setAdapter(myaAdapter); mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 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 } }); }
public class CItem { private int ID; private String Valuehttp://www.mamicode.com/= ""; public CItem() { ID = 0; Value = ""; } public CItem(int _ID, String _Value) { ID = _ID; Value = _Value; } @Override public String toString() { return Value; } public int GetID() { return ID; } public String GetValue() { return Value; }}
Spinner数据绑定方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。