在一个带有两个片段的小型应用程序上工作,第二个片段包含几个微调器。我想将用户选择从微调器传递到第一个Fragment中的recyclerView。而且我被困住了。
在此处查看Android文档:Spinner Doc
该文档的最后一句话说:
如果您通过Activity或Fragment实现AdapterView.OnItemSelectedListener接口(例如在上面的示例中),则可以将其作为接口实例传递。
我是一个肮脏的新手。...我不明白“将其作为接口实例传递”的含义
这是我第二个片段中的微调代码
CreateNewJobFragment.kt
//String array. Move this to res/strings
val theTown = arrayOf("Dublin", "Houston", "Paris", "Auckland", "Bismark")
val townSpinner = binding.spinnerLocation
//Adapter for spinner
townSpinner.adapter = ArrayAdapter(activity, android.R.layout.simple_spinner_dropdown_item, theTown)
//item selected listener for spinner
townSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onNothingSelected(p0: AdapterView<*>?) {
TODO("not implemented yet")
}
override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
Toast.makeText(activity, theTown[p2], Toast.LENGTH_LONG).show()
nameSpinner.onItemSelectedListener = this
}
}
我的newb问题是如何处理'nameSpinner.onItemSelectedListener = this'?
我有一个适配器设置,目前正将伪数据传递到我的recyclerView
JobAdapter.kt
class JobAdapter(priv val jobList:ArrayList):RecyclerView.Adapter(){
//Returning view for each item in the list
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): JobAdapter.ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.job_list_item, parent, false)
return ViewHolder(v)
}
//Binding the data on the list
override fun onBindViewHolder(holder: JobAdapter.ViewHolder, position: Int) {
holder.bindItems(jobList[position])
}
override fun getItemCount(): Int {
return jobList.size
}
//Class holds the job list view
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bindItems(job: JobData) {
val textViewCompnayName = itemView.findViewById(R.id.tv_companyName) as TextView
val textViewLocation = itemView.findViewById(R.id.tv_job_location) as TextView
textViewCompnayName.text = job.companyName
textViewLocation.text = job.location
}
}
}
还有一个数据类。
JobData.kt
data class JobData(val companyName: String,
val location: String)
我相当...我需要使用我的JobAdapter来处理此问题。但是我无法使其正常工作。我也在查看ViewModel,但还没有弄清楚如何进行。
有人想指出我正确的方向吗?