SimpleListAdapter.kt
TLDR
The SimpleListAdapter
class is an implementation of BaseAdapter
that can be used to display a list of items in an Android ListView
. It provides a way to bind data to views and inflate the layout for each list item.
Classes
SimpleListAdapter
The SimpleListAdapter
class is a generic class that extends the BaseAdapter
class. It is used to create an adapter for a ListView
that displays a list of items of type T
.
- It takes three parameters:
-
rowResId
: the resource ID of the layout file used for each list item -
items
: a list of items of typeT
to be displayed in theListView
-
bindView
: a lambda expression that defines how each list item view should be bound to its corresponding data item
-
- The
getView
method is overridden to provide a view for each list item. If a recycled view is available, it is reused, otherwise, a new view is inflated using the providedrowResId
. ThebindView
lambda is then called to bind the view to its data. - Other methods from the
BaseAdapter
class (getItem
,getItemId
,getCount
) are also overridden to provide the necessary functionality for the adapter.
package leakcanary.internal.activity.ui
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import leakcanary.internal.navigation.inflate
internal class SimpleListAdapter<T>(
private val rowResId: Int,
private val items: List<T>,
private val bindView: SimpleListAdapter<T>.(View, Int) -> Unit
) : BaseAdapter() {
override fun getView(
position: Int,
convertView: View?,
parent: ViewGroup
): View {
val view = convertView ?: parent.inflate(rowResId)
bindView(view, position)
return view
}
override fun getItem(position: Int) = items[position]
override fun getItemId(position: Int) = position.toLong()
override fun getCount() = items.size
}