Faced the following, in all guides the proposed methods for obtaining the position \ text from the spinner do not work.

 ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(this, R.array.importance_list, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); Log.v("ΠΈΡ‚Π΅ΠΌ","item = "+spinner.getSelectedItem().toString()); switch (spinner.getSelectedItem().toString()) { case "0": Log.v("ΠΈΡ‚Π΅ΠΌ","item = 0"); intent = new Intent(); intent.putExtra("importance", "no_matter"); break; case "1": Log.v("ΠΈΡ‚Π΅ΠΌ","item = 1"); intent = new Intent(); intent.putExtra("importance", "green"); Log.v("ΠΈΡ‚Π΅ΠΌ","item = 2"); break; case "2": intent = new Intent(); intent.putExtra("importance", "yellow"); Log.v("ΠΈΡ‚Π΅ΠΌ","item = 3"); break; case "3": intent = new Intent(); intent.putExtra("importance", "red"); Log.v("ΠΈΡ‚Π΅ΠΌ","item = 4"); break; default: intent = new Intent(); intent.putExtra("importance", "no_matter"); Log.v("ΠΈΡ‚Π΅ΠΌ","Π΄Π΅Ρ„ΠΎΠ»Ρ‚"); break; } 

Console:

 10-23 22:31:27.153 12415-12415/com.asgard.smartnotes V/ΠΈΡ‚Π΅ΠΌ: item = НС Π²Π°ΠΆΠ½ΠΎ 10-23 22:31:27.153 12415-12415/com.asgard.smartnotes V/ΠΈΡ‚Π΅ΠΌ: Π΄Π΅Ρ„ΠΎΠ»Ρ‚ 

I would understand if you could see anything through the console without logging in to the switch - but here it simply doesn’t respond to pressing.

  • Emm ... if you have spinner.getSelectedItem().toString() == 'НС Π²Π°ΠΆΠ½ΠΎ' , then default will be executed in the switch-case . - post_zeew
  • The @post_zeew thing is that the logs do not show the values ​​that come after changing the list item. I believe that nothing comes at all. - Silento
  • Do you want to perform some action immediately after the user selects an item in the spinner ? - post_zeew 6:49 pm
  • @post_zeew Yes, the intent is going, first passing through the switch, then the rest of the data - and returning it to onActivityResult , that is, to collect - to transfer. - Silento
  • one
    OnItemSelectedListener triggered immediately after selecting a certain spinner item. And with the help of getSelectedItem() you can get the selected item at any time. - post_zeew

1 answer 1

The Spinner class's getSelectedItem() method returns the selected spinner item .

The getSelectedItem().toString() method chain returns the textual representation of the selected item.

The getSelectedItemPosition() method returns the ordinal (starting from zero) number of the selected item.