0

In android I used a spinner to select an item from a list of items. Now I have to use that selected item in java. How can I do that?

Tony Baby
  • 7,176
  • 6
  • 18
  • 22

2 Answers2

4

Try this!!!

spin.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            String yourName=spiner.getSelectedItem().toString();

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

If code above not help you. Try this!!

Spinner spinner = (Spinner)findViewById(R.id.spinner);
String text = spinner.getSelectedItem().toString();
Nguyễn Trung Hiếu
  • 2,004
  • 1
  • 10
  • 22
-1

In Java, you can using JComboBox to display list item and get selected. Ex:

String[] numbers= { "One", "Two", "Three", "Four", "Five" };
//Create the combo box, select item at index 4.
//Indices start at 0, so 4 specifies the 'Five'.
JComboBox clst= new JComboBox(numbers);
petList.setSelectedIndex(4);
petList.addActionListener(this);
KunMyt
  • 32
  • 5