选择微调器项目时使用位置获取对象(onItemSelectedListener)

时间:2019-06-19 12:43:05

标签: android kotlin android-spinner android-mvvm

我有两个微调器。第一个微调器(projectSpinner)使用数据contentSpinner的字段project_id填充第二个(List<Project>)。在非组件架构中,我只需将List<Project>存储在活动中,然后在onItemSelectedListener内部进行类似Project[position].project_id的操作,以帮助我填充第二个微调器({{1 }})

现在正在尝试采用MVVM体系结构,我无法弄清楚如何获得此contentSpinner

这是我到目前为止所拥有的:

project_id
projectViewModel = ViewModelProviders.of(this).get(SpinnerProjectViewModel::class.java)
spinnerModuleViewModel = ViewModelProviders.of(this).get(SpinnerModuleViewModel::class.java)
projectViewModel.getProjectData()?.observe(this, Observer<List<Project>> { list: List<Project>? ->
            //create adapter for project spinner
            projectSpinnerCustomAdapter = ProjectSpinnerCustomAdapter(this, list!!)
            projectSpinner?.adapter = projectSpinnerCustomAdapter
        })

        projectSpinner?.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {
                Toast.makeText(this@AddRecordingActivity, "No video selected", Toast.LENGTH_LONG).show()
            }

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

我传递给 private fun bindModuleSpinnerData(position: Int){ spinnerModuleViewModel.getSpinnerModuleData(10738)?.observe( this, Observer {contentList: List<Content>? -> val contentSpinnerCustomAdapter = ContentSpineerCustomAdapter(this@AddRecordingActivity, contentList!!) contentSpinner!!.adapter = contentSpinnerCustomAdapter }) } 函数projectId的{​​{1}}已被硬编码。

如何从我的ViewModel函数中获得此(spinnerModuleViewModel.getSpinnerModuleData(10738))? 还是有更好的方法来实现这一目标?

0 个答案:

没有答案