android kotlin spinner working for API 23, but not working for API 21

淺唱寂寞╮ 提交于 2019-12-13 03:46:35

问题


I test my spinner in real device SM-G900F API 23, it is working well. But it is not working in SM-N9005 API 21. It shows the spinner but it did not show any options when I click the spinner.

Please see my code below and help to solve this issue.

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

        val spinnerOne = search_spinner1
        val spinnerTwo = search_spinner2
        val userDetail = UserDetail(this)
        //SpinnerOne
        spinnerOne.adapter = ArrayAdapter<String>(this, R.layout.item, countryArray)
        //SpinnerTwo
        val dataAdapter = ArrayAdapter<String>(this, R.layout.item, tempArray)
        spinnerTwo.setAdapter(dataAdapter)
        spinnerOne.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {
            }
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                countrySelected = parent?.getItemAtPosition(position).toString()
                citySelected = countrySelected
                if (countrySelected == "香港") {
                    userDetail.saveCountry(countrySelected)
                    userDetail.saveCity(countrySelected)
                    spinnerTwo.visibility = View.GONE
                    finish()
                } else if (countrySelected == "澳門") {
                    userDetail.saveCountry(countrySelected)
                    userDetail.saveCity(countrySelected)
                    spinnerTwo.visibility = View.GONE
                    finish()
                } else if (countrySelected == "台灣") {
                    userDetail.saveCountry(countrySelected)
                    tempArray.clear()
                    tempArray.addAll(taiwanArray)
                    dataAdapter.notifyDataSetChanged()
                    spinnerTwo.visibility = View.VISIBLE
                } 
            }
        }

        //hide spinner2
        spinnerTwo.visibility = View.GONE
        spinnerTwo.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {
            }
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                citySelected = parent?.getItemAtPosition(position).toString()
                if (citySelected == "選擇城市") {
                } else {
                    userDetail.saveCity(citySelected)
                    finish()
                }
            }
        }

Here with the xml file:

enter code here


回答1:


I found the answer

android:spinnerMode="dialog"

add this code it will be work for API 21 and 23



来源:https://stackoverflow.com/questions/51495271/android-kotlin-spinner-working-for-api-23-but-not-working-for-api-21

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!