聪明地投向“旋转者!”是不可能的。因为'spinnertype'是一个可变的属性,这一次可能已经更改了

时间:2019-10-01 17:03:58

标签: android kotlin android-spinner

我是第一次使用Kotlin制作android应用, 我想问你们为什么'spinnerType.onItemSelectedListener'这行出错。

这是我的AddActivity.kt。

   override fun onCreate(savedInstanceState: Bundle?) {
    spinnerType = findViewById(R.id.spinner_type)
    spinnerCategory = findViewById(R.id.spinner_category)
    editTextAmount = findViewById(R.id.edit_text_amount)
    editTextNote = findViewById(R.id.edit_text_note)

    // binding adapter and listener
    expendCategoryList = object : ArrayList<String>() {
        init {
            add("aaa")
            add("bbb")
            add("ccc")
        }
    }
    incomeCategoryList = object : ArrayList<String>() {
        init {
            add("dd")
            add("ee")
        }
    }

    bindSpinnerCategoryData(true)

    // bind event listener about spinnerType
    spinnerType.onItemSelectedListener
   }



    private fun bindSpinnerCategoryData(isExpend: Boolean) {
    val arrayAdapter: ArrayAdapter<String>
    val targetList = if (isExpend) expendCategoryList else incomeCategoryList
    val list = ArrayList(targetList)
    list.add("add new category...")
    arrayAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, list)
    arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
    // new data adpater binding aboutspinnerCategory
    spinnerCategory.setAdapter(arrayAdapter)
}

1 个答案:

答案 0 :(得分:0)

onItemSelectedListener没有正确实现,请尝试以下代码可以解决您的问题

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