# Kotlin , android spinner onItemSelectedListener doesn't work

**URL:** <https://discuss.kotlinlang.org/t/kotlin-android-spinner-onitemselectedlistener-doesnt-work/11771>\
**Category:** Android\
**Created:** [February 28, 2019, 8:50am UTC](https://discuss.kotlinlang.org/t/kotlin-android-spinner-onitemselectedlistener-doesnt-work/11771 "2019-02-28T08:50:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kldmohammed](https://avatars.discourse-cdn.com/v4/letter/k/9de053/32.png) [@kldmohammed](https://discuss.kotlinlang.org/u/kldmohammed)\
**Post date:** [February 28, 2019, 8:50am UTC](https://discuss.kotlinlang.org/t/kotlin-android-spinner-onitemselectedlistener-doesnt-work/11771/1 "2019-02-28T08:50:39Z")

</div>

Am android developer and i always had a problem with spinner and its listener  
this is my code  
stateSpinner?.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {  
override fun onNothingSelected(parent: AdapterView\<\*\>?) {  
toast(“Please select state”)  
}

```
        override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        
            // val state = stateSpinner.selectedItem as States
            val state = parent?.getItemAtPosition(position) as States
            // toast("state id :${state.id} , state name :${state.arName}")
        
            Log.d("ccccc" , state.toString())
        
                stateId = state.id
            
                loadCities(stateId)
         
        }
    }

```

i write it and tested its works and after couple of days the listener not triggered or called and when i select any item of spinner nothing happens either logcat or toast or anything .

whats happening here ?

---

<div class="post-metadata">

**Author:** ![thesim0n1989](https://avatars.discourse-cdn.com/v4/letter/t/c89c15/32.png) [@thesim0n1989](https://discuss.kotlinlang.org/u/thesim0n1989)\
**Post date:** [February 28, 2019, 4:05pm UTC](https://discuss.kotlinlang.org/t/kotlin-android-spinner-onitemselectedlistener-doesnt-work/11771/2 "2019-02-28T16:05:26Z")

</div>

StackOverflow might be a better place to ask this kind of questions.

Anyway. From where are you calling “stateSpinner?..” ? From a fragment? Do you use kotlin synthetic and calling it from onCreateView instead of onViewCreated and stateSpinner is actually null? Log if stateSpinner is null. Just a wild guess.

---

<div class="post-metadata">

**Author:** ![kldmohammed](https://avatars.discourse-cdn.com/v4/letter/k/9de053/32.png) [@kldmohammed](https://discuss.kotlinlang.org/u/kldmohammed)\
**Post date:** [March 1, 2019, 12:15am UTC](https://discuss.kotlinlang.org/t/kotlin-android-spinner-onitemselectedlistener-doesnt-work/11771/3 "2019-03-01T00:15:23Z")

</div>

first i put it in fragment and called in onViewCreated and as i said its stopped without i touch this fragment , and i thought that the problem from the fragment , and changed to activity and also nothing work here , someone in a telegram group hinted me that maybe the problem from the scroll view if i put it in a scroll view , i delete scroll view and nothing change

---

<div class="post-metadata">

**Author:** ![thesim0n1989](https://avatars.discourse-cdn.com/v4/letter/t/c89c15/32.png) [@thesim0n1989](https://discuss.kotlinlang.org/u/thesim0n1989)\
**Post date:** [March 1, 2019, 12:51pm UTC](https://discuss.kotlinlang.org/t/kotlin-android-spinner-onitemselectedlistener-doesnt-work/11771/4 "2019-03-01T12:51:39Z")

</div>

Did you fix it? Could you show me where you define stateSpinner and could you log if the spinner is acutally not null?

---

<div class="post-metadata">

**Author:** ![mijcon](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@mijcon](https://discuss.kotlinlang.org/u/mijcon)\
**Post date:** [March 4, 2019, 7:53am UTC](https://discuss.kotlinlang.org/t/kotlin-android-spinner-onitemselectedlistener-doesnt-work/11771/5 "2019-03-04T07:53:14Z")

</div>

Does your class “State” inherit from class “View”?

If not, then I suspect you may want to try replacing  
`val state = parent?.getItemAtPosition(position) as State`  
with  
`val state = statesList[position]`

If you give us a rough idea of what your State class looks like, we may be able to help more.

---

<div class="post-metadata">

**Author:** ![rajg.naga](https://avatars.discourse-cdn.com/v4/letter/r/ecd19e/32.png) [@rajg.naga](https://discuss.kotlinlang.org/u/rajg.naga)\
**Post date:** [September 28, 2019, 10:28am UTC](https://discuss.kotlinlang.org/t/kotlin-android-spinner-onitemselectedlistener-doesnt-work/11771/6 "2019-09-28T10:28:44Z")

</div>

How do I bind Map\<String, String\> to AppCompatSpinner via data binding .  
And also onItemSelected does not seem to exist for AppCompatSpinner from AndroidX.  
I know it to set from ArrayAdapter , but i want to move UI logic code to xml than activity or fragments

---

<div class="post-metadata">

**Author:** ![kldmohammed](https://avatars.discourse-cdn.com/v4/letter/k/9de053/32.png) [@kldmohammed](https://discuss.kotlinlang.org/u/kldmohammed)\
**Post date:** [October 19, 2019, 5:24pm UTC](https://discuss.kotlinlang.org/t/kotlin-android-spinner-onitemselectedlistener-doesnt-work/11771/7 "2019-10-19T17:24:06Z")

</div>

I replaced spinner with dialog , but i will let you know how my State class looks like

`data class State ( val id :Int , val nameEn : String , val nameEr : String)`

---

<div class="post-metadata">

**Author:** ![kldmohammed](https://avatars.discourse-cdn.com/v4/letter/k/9de053/32.png) [@kldmohammed](https://discuss.kotlinlang.org/u/kldmohammed)\
**Post date:** [October 19, 2019, 5:24pm UTC](https://discuss.kotlinlang.org/t/kotlin-android-spinner-onitemselectedlistener-doesnt-work/11771/8 "2019-10-19T17:24:45Z")

</div>

Sorry bro I don’t use data binding
