Here's the complete process in Kotlin:
the spinner adapter class:
class CustomSpinnerAdapter(
context: Context,
textViewResourceId: Int,
val list: List<User>
) : ArrayAdapter<User>(
context,
textViewResourceId,
list
) {
override fun getCount() = list.size
override fun getItem(position: Int) = list[position]
override fun getItemId(position: Int) = list[position].report.toLong()
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
return (super.getDropDownView(position, convertView, parent) as TextView).apply {
text = list[position].name
}
}
override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
return (super.getDropDownView(position, convertView, parent) as TextView).apply {
text = list[position].name
}
}
}
and use it in your activity/fragment like this:
spinner.adapter = CustomerSalesSpinnerAdapter(
context,
R.layout.cuser_spinner_item,
userList
)