Clevers Rungene


Clevers Rungene


Political Preparedness

Part 3a:Election-adapter

Clevers Rungene's photo
Clevers Rungene
·Oct 22, 2021·

2 min read

Political Preparedness


In regard to computing an adapter can be either a hardware component or software that allows two or more incompatible devices to be linked together ,without loss of function, for the purpose of transmitting and receiving data.

Adapter converts one interface to work with another. In an android recyclerview context, an adapter is a subclass of RecyclerView.Adapter responsible for providing views that represent items in a data set.


Adapter Interface

  • How many items?
  • Creates a new ViewHolder
  • Draws items into ViewHolder

Adapter Interface.jpg


We use ListAdapter over RecyclerView.Adapter which supports list diffing and animates nicely for new list items.

It computes Diffs on background thread which means we don’t need to implement it by ourselves. Also, it provides more convenient and easy implementation of DiffUtil rather than implementing diffs with normal RecyclerView.Adapter.

Unlike RecyclerView.Adapter implementation, we don’t need to override getItemCount() method as ListAdapter manages the list for us. So we only need to implement two methods : onCreateViewHodler() and onBindViewHolder() .

Learn more from this medium post on ListAdapter


import android.view.LayoutInflater.from
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView

class ElectionListAdapter(private val clickListener: ElectionListener):
//Use ListAdapter over RecyclerView.Adapter which supports
// list diffing and animates nicely for new list items
    ListAdapter<Election, ElectionViewHolder>(ElectionDiffCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int):
            ElectionViewHolder {
        return from(parent)

    //Bind ViewHolder

    override fun onBindViewHolder(holder: ElectionViewHolder, position: Int) {
        val anElection = getItem(position)

        //Setting up click listener


    // Add companion object to inflate ViewHolder (from)

    companion object {
        fun from(parent: ViewGroup):ElectionViewHolder {
            val layoutInflater = from(parent.context)
            val electionListItemBinding = ElectionListItemBinding.inflate(layoutInflater,parent,false)
            return ElectionViewHolder(electionListItemBinding)


// Create ElectionViewHolder
class ElectionViewHolder(var binding: ElectionListItemBinding):
    fun bind(election: Election) {
        binding.election = election

        // Call binding.executePendingBindings(), which causes the update to execute immediately.

 * Callback for calculating the diff between two non-null items in a list.
 * Used by ListAdapter to calculate the minimum number of changes between new and old list and a new
 * list that's been passed to `submitList`.

// Create ElectionDiffCallback
class ElectionDiffCallback: DiffUtil.ItemCallback<Election>() {
    override fun areItemsTheSame(oldItem: Election, newItem: Election): Boolean {
        return ==

    override fun areContentsTheSame(oldItem: Election, newItem: Election): Boolean {
        return oldItem == newItem

// Create ElectionListener
class ElectionListener(val clickListener: (electionId: Election) -> Unit) {
    fun onClick(election: Election) = clickListener(election)

Check out ElectionListAdapter.kt on GitHub

Many thanks for reading see you on part 3b

Share this