2020-07-01-Anroid-Kotlin-ListView-ImageDownload

build.gradle (Module: app)

dependencies {
    ...
    implementation 'com.github.bumptech.glide:glide:4.9.0'
    ...
}

MainListAdaptor.kt

class ListItem(
    val title: String,
    val subtitle: String,
    val imageURL: String
)

class MainListAdaptor(
    private val context: Activity,
    private val list: List<ListItem>
): ArrayAdapter<ListItem>(context, R.layout.list_item_main, list) {

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val inflator = context.layoutInflater
        val view = inflator.inflate(R.layout.list_item_main, null, true)

        // update UI
        view.titleText.text = list[position].title
        view.subtitleText.text = list[position].subtitle
        // ----------

        // Download Image and set it in listView
        Glide.with(view)  // glide with this view
            .load(list[position].imageURL) // download using this url
            .override(250, 250) // set this size
             .placeholder(R.mipmap.ic_launcher) // default placeHolder image
             .error(R.mipmap.ic_launcher) // use this image if faile
            .into(view.imageView) // set 

        return view
    }
}

MainActivity.kt

Last updated

Was this helpful?