'Why the spinner function cannot select item?
I have faced a problem which my spinner cannot show the item selected in the page. But for the selection of "All", it can show normally all item but when I click one of the selection in spinner, it will show nothing for me no matter the selection name is match with job name or not. Can some one help me?
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")
}
}
}
Solution 1:[1]
Try to change your onItemSelectedListener logic:
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")
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | lpizzinidev |
