Home > OS >  How can I change (with Kotlin) which item my spinner selects?
How can I change (with Kotlin) which item my spinner selects?

Time:10-27

function

    private fun updateCurrencySpinner() {
// Update selected item form the currency spinner based on the country spinner
val currency = when (serviceSpinner.selectedItemPosition) {
    0 -> 0
    1 -> 0
    2 -> 1
    3 -> 2
    else -> 0
}
currenciesSpinner.setSelection(currency)
}

when i call the function

        countrySpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
    override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        updateCurrencySpinner()
    }

    override fun onNothingSelected(parent: AdapterView<*>?) {}
}

can someone tell me why my spinner does not update (I call this function every time I change the selected item)?

CodePudding user response:

You call updateCurrencySpinner when countrySpinner changes, but you don't use the value of countrySpinner, instead you use the value of serviceSpinner.

So even if you call the method when countrySpinner changes, it does not actually change anything because the value of serviceSpinner is still the same.

  • Related