我有两个微调器。第一个微调器(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)
)?
还是有更好的方法来实现这一目标?