Android:Spinner:在代码中设置ArrayAdapter不需要调用onItemSelected,只在用户单击spinner时调用

u5rb5r59  于 12个月前  发布在  Android
关注(0)|答案(1)|浏览(101)

Android Spinner:当我在代码中设置ArrayAdapter时,Android调用onItemSelected。只有当用户单击微调项时才可能调用?
收听者

binding.spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
        override fun onItemSelected(adapterView: AdapterView<*>?, view: View, i: Int, l: Long) {
            value = binding.spinner.selectedItem.toString()

            when (value) {
                'a' -> {
                    valueId = 1
                    Log.d("onItemSelected", "valueId = 1")
                }
                'b' -> {
                    valueId = 2
                    Log.d("onItemSelected", "valueId = 2")
                    
                }
                else -> {
                    valueId = 0
                    Log.d("onItemSelected", "valueId = 0")
                }
            }
        }

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

字符串
旋转调整器

val values = arrayOf('Choice : ', 'a', 'b', 'c')  
val adapterSpinner = ArrayAdapter(mContext, android.R.layout.simple_spinner_dropdown_item, values)
            binding.spinner.adapter = adapterSpinner


是否可以用其他解决方案更改微调器上的值?

ymdaylpp

ymdaylpp1#

我找到了解决方案。这是可能的Android Spinner与ListPopupWindow的替代品。我可以改变适配器,而无需调用事件onItemSelected

相关问题