1

I need to retrieve the selected string in a spinner outside of the .onItemSelectedListener. The dropdown menu contains "Each week, each month, each year" strings and I need to retrieve that selected item String in order use them in if conditionals outside of the function.

I've only seen people making Toasts in the onItemSelected function but this doesn't solve my problem.

This is my code:

val spinner = binding.tvAutoComplete
val powtarzanie = resources.getStringArray(R.array.powtarzanie)
val arrayAdapter = ArrayAdapter(requireContext(),
                   R.layout.dropdown_powtarzaj_item,
                   powtarzanie)
spinner.setAdapter(arrayAdapter)
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
    override fun onItemSelected(
        parent: AdapterView<*>?,
        view: View?,
        position: Int,
        id: Long
    ) {
       val itemText: String = parent?.getItemAtPosition(position).toString()

    }

    override fun onNothingSelected(parent: AdapterView<*>?) {
        TODO("Not yet implemented")
    }
}
Paula
  • 41
  • 4
  • Does this answer your question? [Get the position of a spinner in Android](https://stackoverflow.com/questions/8597582/get-the-position-of-a-spinner-in-android) – Firoz Memon Nov 14 '22 at 11:15
  • You can create global variable and assign the value from onItemSelectedListener, does this helps? – Aditya Nandardhane Nov 14 '22 at 11:17
  • @FirozMemon it doesn't :( – Paula Nov 14 '22 at 11:33
  • @AdityaNandardhane How do I create global variable in kotlin? I've tried doing the lateinit var at the top of my class, then assign it in the .onItemSelectedListener and then use this variable, but there is error when launching the app, says that lateinit property has not been initialized – Paula Nov 14 '22 at 11:35
  • and I totally get it why there's an error, I just don't know how to get that string to use it outside – Paula Nov 14 '22 at 11:36

1 Answers1

0

One way and probably the most simple way to go about this is to define a global variable:

private var spinnerSelection: String? = null

and just change it in your onItemSelected implementation:

spinnerSelection = parent?.getItemAtPosition(pos).toString()

Example code showing the whole flow:

class MainActivity : AppCompatActivity(), AdapterView.OnItemSelectedListener {

    private var spinnerSelection: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val spinner: Spinner = findViewById(R.id.spinner)
        spinner.onItemSelectedListener = this
        ArrayAdapter.createFromResource(
            this,
            R.array.numbers_array,
            android.R.layout.simple_spinner_item
        ).also { adapter ->
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            spinner.adapter = adapter
        }

        val button: Button = findViewById(R.id.button)

        button.setOnClickListener {
            Log.d("MainActivity", "Currently selected item: $spinnerSelection")
        }
    }

    override fun onItemSelected(parent: AdapterView<*>?, view: View?, pos: Int, id: Long) {
        spinnerSelection = parent?.getItemAtPosition(pos).toString()
    }

    override fun onNothingSelected(parent: AdapterView<*>?) {
        //TODO("Not yet implemented")
    }
}
  • It worked by changing `spinnerSelection = parent?.getItemAtPosition(pos).toString()` to `array[position].toString()` – Paula Jan 11 '23 at 16:49