0
I have a custom spinner, I am correctly assigning the name, but the image is repeating in all items on the list.
class Server(val context: Context, var servers: Array<ServerCode>) : BaseAdapter() {
private val mInflater: LayoutInflater = LayoutInflater.from(context)
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val view: View
val vh: ItemHolder
if (convertView == null) {
view = mInflater.inflate(R.layout.itemserver, parent, false)
vh = ItemHolder(view)
view?.tag = vh
} else {
view = convertView
vh = view.tag as ItemHolder
}
vh.nameServer.text = servers[position].name
if (servers[position].name == ServerCode.Na.name) {
vh.imageServer.setBackgroundResource(R.drawable.us)
} else {
vh.imageServer.setBackgroundResource(R.drawable.eubr)
}
return view
}
override fun getItem(position: Int): Any? {
return servers[position]
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getCount(): Int {
return servers.size
}
private class ItemHolder(row: View?) {
val nameServer: TextView = row?.findViewById(R.id.lblServerName) as TextView
val imageServer: ImageView = row?.findViewById(R.id.imgServer) as ImageView
}
}
enum class ServerCode {
Na, EU
}
It looks like this: (In the "Na" should be the US flag, as set in the Adapter)