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 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)