微调框未打开

时间:2019-02-19 11:15:32

标签: android android-fragments kotlin android-spinner

单击下拉菜单时,微调器没有打开。有帮助吗? 我的项目列表在字符串资源中。

class CreateIssueFragment : Fragment() {

var spinner: Spinner? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val rootView: View = inflater.inflate(R.layout.fragment_create_issue, container, false)
    spinner = activity?.issueTypesSpinner
    loadSpinner()
    return rootView
}

private fun loadSpinner() {
    ArrayAdapter.createFromResource(context, R.array.issue_types_array, android.R.layout.simple_spinner_item)
            .also { adapter ->
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
                spinner?.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
                    override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
                        parent.getItemAtPosition(position).toString()
                    }

                    override fun onNothingSelected(parent: AdapterView<*>) {
                    }
                }
                spinner?.adapter = adapter

            }
}
}

2 个答案:

答案 0 :(得分:0)

尝试这样替换:

yourSpinner?.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
    override fun onNothingSelected(parent: AdapterView<*>?) {

    }

    override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {

    }

}

答案 1 :(得分:0)

class CreateIssueFragment : Fragment() {


override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val rootView: View = inflater.inflate(R.layout.fragment_create_issue, container, false)
    return rootView
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    loadSpinner()

}

private fun loadSpinner() {
    val list = context?.resources?.getStringArray(R.array.issue_types_array)


    ArrayAdapter.createFromResource(context, R.array.issue_types_array, android.R.layout.simple_spinner_item)
            .also { adapter ->
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
                issueTypesSpinner?.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {

                    override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
                        parent.getItemAtPosition(position)
                    }

                    override fun onNothingSelected(parent: AdapterView<*>) {
                    }
                }
                issueTypesSpinner?.adapter = adapter
            }

}

}