我遇到了一个问题,我的旋转器无法显示页面中选择的项目。但是对于"全部"的选择,它可以正常显示所有的项目,但是当我在spinner中单击其中一个选择时,无论选择名称是否与作业名称匹配,它都不会显示任何内容。有人能帮帮我吗?
override fun onResume() {
super.onResume()
val places=resources.getStringArray(R.array.place)
val arrayAdapter=ArrayAdapter(requireContext(),R.layout.dropdown_item,places)
val jobs=resources.getStringArray(R.array.job)
val arrayAdapter2=ArrayAdapter(requireContext(),R.layout.dropdown_item,jobs)
binding.spinnerPlace.setAdapter(arrayAdapter)
binding.spinnerJob.setAdapter(arrayAdapter2)
spinner_job.onItemSelectedListener=object : AdapterView.OnItemSelectedListener{
override fun onItemSelected(
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long
) {
if (parent != null) {
if(parent.getItemAtPosition(position)=="All"){
tempArrayList.addAll(newArrayList)
val adapter=Search_Page_Adapter(requireContext(),tempArrayList)
newRecyclerView.adapter=adapter
newRecyclerView.layoutManager=LinearLayoutManager(requireContext())
}
else{
tempArrayList.clear()
val item=parent.getItemAtPosition(position).toString()
Toast.makeText(parent.context,"Selected:$item",Toast.LENGTH_SHORT).show()
if(item.isNotEmpty()){
newArrayList.forEach{
if(it.jobname?.lowercase(Locale.getDefault())!!.contains(item)){
tempArrayList.add(it)
}
val adapter=Search_Page_Adapter(requireContext(),tempArrayList)
newRecyclerView.adapter=adapter
newRecyclerView.layoutManager=LinearLayoutManager(requireContext())
}
}else{
tempArrayList.clear()
tempArrayList.addAll(newArrayList)
newRecyclerView.adapter!!.notifyDataSetChanged()
}
}
}
}
override fun onNothingSelected(parent: AdapterView<*>?) {
TODO("Not yet implemented")
}
}
}
尝试更改您的onItemSelectedListener
逻辑:
spinner_job.onItemSelectedListener=object : AdapterView.OnItemSelectedListener{
override fun onItemSelected(
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long
) {
val item = parent.getItemAtPosition(position).toString()
if(item == "All") {
// `All` selected
tempArrayList.addAll(newArrayList)
val adapter=Search_Page_Adapter(requireContext(),tempArrayList)
newRecyclerView.adapter=adapter
newRecyclerView.layoutManager=LinearLayoutManager(requireContext())
}
else {
// Others selected
Toast.makeText(parent.context,"Selected:$item",Toast.LENGTH_SHORT).show()
if(item.isNotEmpty()){
newArrayList.forEach{
if(it.jobname?.lowercase(Locale.getDefault())!!.contains(item)){
tempArrayList.add(it)
}
val adapter=Search_Page_Adapter(requireContext(),tempArrayList)
newRecyclerView.adapter=adapter
newRecyclerView.layoutManager=LinearLayoutManager(requireContext())
}
} else {
tempArrayList.clear()
tempArrayList.addAll(newArrayList)
newRecyclerView.adapter!!.notifyDataSetChanged()
}
}
}
override fun onNothingSelected(parent: AdapterView<*>?) {
TODO("Not yet implemented")
}