• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Adding text to an imageview in a BaseAdapter

Pallonne

Lurker
I have a BaseAdapter I am using to put up some images in a gridview which will be used for categories of a sort. When adding the adapter to the gridview I can currently only get the images to show up on my activity. I would also like to have a small text box below the image with the name of the category. So the finished grid will be something like 2x6 with an image of lets say a movie and the name of the movie under the image. My adapter code currently looks like below. What's the easiest way of adding some text below the images?

Code:
package com.example.recommended

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import com.example.recommended.R
import kotlinx.android.synthetic.main.list_item.view.*

class ImageAdapter internal constructor(private val mContext: Context) : BaseAdapter() {

    // References to our images
    private val mThumbIds = arrayOf(
        R.drawable.sample_0,
        R.drawable.sample_1,
        R.drawable.sample_2,
        R.drawable.sample_3,
        R.drawable.sample_4,
        R.drawable.sample_5,
        R.drawable.sample_6,
        R.drawable.sample_7)

    override fun getCount(): Int {
        return mThumbIds.size
    }

    override fun getItem(position: Int): Any? {
        return null
    }

    override fun getItemId(position: Int): Long {
        return 0
    }

    // Create a new ImageView for each item referenced by the Adapter
    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val imageView: ImageView
        if (convertView == null) {
            // if it's not recycled, initialize some attributes
            imageView = ImageView(mContext)
            imageView.layoutParams = ViewGroup.LayoutParams(300, 300)
            imageView.scaleType = ImageView.ScaleType.CENTER_CROP
        } else {
            imageView = (convertView as ImageView?)!!
        }

        imageView.setImageResource(mThumbIds[position])
        return imageView
    }
}
 
Back
Top Bottom