Kotlin ImageView Problem With GetView Function Type Mismatch

I have having problems on the fourth line. In my code, getLogo returns an integer (ID) of an image. I now want to use the drawable ID for the image and put in in a ImageView with the specified id. However, on the left hand of the equal sign, I am getting the error of variable expected and on the right side it says Type Mismatch ... Required: ImageView Found: Int.

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
    val layoutInflator = LayoutInflater.from(context)
    val row = layoutInflator.inflate(R.layout.layoutlistviewloan, parent, false)
    row.findViewById<ImageView>(R.id.institutionImage) = loansModel.loan[position].institution.getLogo()
}

Answer

findViewById() is a function that returns a View (in this case you’re saying it’s an ImageView). It’s not a variable (val or var) that you can assign a value to.

I’m guessing you want to find that ImageView in the layout, and then set it to display your drawable ID. So you need to chain a setImageResource call to the ImageView after you grab it

row.findViewById<ImageView>(R.id.institutionImage).setImageResource(drawableId)

you could stick the “getting the drawable ID” code in there as the setImageResource parameter

row.findViewById<ImageView>(R.id.institutionImage).setImageResource(loansModel.loan[position].institution.getLogo())

but personally I’d just set that as a variable to make it more readable instead of doing too much in one line (it can get confusing!)

val logoId = loansModel.loan[position].institution.getLogo()
row.findViewById<ImageView>(R.id.institutionImage).setImageResource(logoId)