androidkotlincasespinneronitemselectedlistener

How to retrieve the value of a variable modified in the onItemSelected function with Kotlin in Android Studio?


I need to retrieve the value of a variable modified in the onItemSelected function using a when. Here is what I've done :

    (...)

    private var i: Int = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        (...)

        categorySpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            @SuppressLint("SetTextI18n")
            override fun onNothingSelected(parent: AdapterView<*>?) {

            }

            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                category = categories[position]

                when (category) {
                    "Décès" -> i = 1
                    "Habitat" -> i = 2
                }
            }
        }

        binding.tvtest.text = i.toString()

        val acteSpinner: Spinner = binding.acteCreatingSpinnerActes
        when (i) {
            1 -> {
                val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, actes[actes.indexOf(deces)])
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
                acteSpinner.adapter = adapter
                acte = acteSpinner.selectedItem.toString()
            }
            2 -> {
                val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, actes[actes.indexOf(habitat)])
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
                acteSpinner.adapter = adapter
                acte = acteSpinner.selectedItem.toString()
            }
        }
    }
}

I've tried to retrieve the value of variable I in a layout and I got i = 0.
I want to get the value obtained in the onItemSelected function


Solution

  • Try this

    override fun onCreate(savedInstanceState: Bundle?) {
    
        categorySpinner.onItemSelectedListener = object : ItemSelected{
                        override fun onItemSelected(parent: AdapterView<*>?,   view: View?, position: Int, id: Long) {
                category = categories[position]
    
                when (category) {
                    "Décès" ->  getItemSelected(1)
                    "Habitat" ->  getItemSelected(2)
                }}}
    }
    
    private fun setDataSpinner(i: Int,actes: Actes){
        binding.tvtest.text = i.toString()
        val acteSpinner: Spinner = binding.acteCreatingSpinnerActes
    
        val adapter = ArrayAdapter(this, R.layout.simple_spinner_item, actes)
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        acteSpinner.adapter = adapter
        acte = acteSpinner.selectedItem.toString()
    }
    
    private fun getItemSelected(item: Int){
        when(item){
            1 -> { setDataSpinner(item,actes[actes.indexOf(deces)]) }
            2 -> { setDataSpinner(item,actes[actes.indexOf(habitat)]) }
        }
    }
    
    
    interface ItemSelected: AdapterView.OnItemSelectedListener {
        override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long)
        override fun onNothingSelected(parent: AdapterView<*>?) {}
    }