码迷,mamicode.com
首页 > 其他好文 > 详细

Spinner数据绑定方法

时间:2014-11-10 13:29:50      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   io   color   ar   os   sp   for   

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 Value = "";

    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数据绑定方法

标签:android   style   blog   io   color   ar   os   sp   for   

原文地址:http://www.cnblogs.com/leichentao0905/p/4086837.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!