简体   繁体   中英

How to get selected value from a spinner with two item

how to get selected item name from spinner ..?

this is my code

Spinner sp;
ArrayAdapter<String> adapter;
String numbers[] = { "AM", "PM"};

sp = (Spinner) findViewById(R.id.timing);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, numbers);
sp.setAdapter(adapter);

i want to get the AM /PM value from the spinner if anybody knows please help me

Use OnItemSelectedListener

sp.setOnItemSelectedListener(this);

@Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) 
    {
        String selected = arg0.getItemAtPosition(arg2).toString();
        System.out.println(selected);

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM