Search code examples
androidkotlinandroid-recyclerviewandroid-checkbox

How to display total checkbox checked in a recyclerview


I am trying to create a simple pokedex where you can keep track of how many pokemons you have collected. The trouble I am having is the checkbox in a recyclerview. I want to have the total which is currently at zero, go up for each checkbox checked. If unchecked the number will go down. The total number is simply just shown in a text view.

Below is an image showing what I am trying to explain, in case I am not explaining it as clearly.

enter image description here

Here is my code

Adapter Class

class MainAdapter(val pokeList : List<PokeData>) : RecyclerView.Adapter<MainAdapter.ViewHolder>() {

    var totalCollectedPokemon = 0

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainAdapter.ViewHolder {

        val layoutInflater = LayoutInflater.from(parent.context)
        val view = layoutInflater
            .inflate(R.layout.poke_contents, parent, false)
        return ViewHolder(view)

    }

    override fun getItemCount(): Int = pokeList.size

    override fun onBindViewHolder(holder: MainAdapter.ViewHolder, position: Int) {
        val pokeDetails = pokeList[position]

        holder.checkbox.setOnClickListener {
            if(holder.checkbox.isChecked) {
                totalCollectedPokemon = totalCollectedPokemon + 1
            } else {
                totalCollectedPokemon = totalCollectedPokemon - 1
            }
        }

        holder.bind(pokeDetails)
    }

    inner class ViewHolder(val v: View) : RecyclerView.ViewHolder(v) {

        val pokemonImage = v.findViewById<ImageView>(R.id.pokemon_image)
        val pokemonName = v.findViewById<TextView>(R.id.pokemon_name)
        val collectedPokemon = v.findViewById<TextView>(R.id.total_collected_pokemon)
        val checkbox = v.findViewById<CheckBox>(R.id.checkBox)

        fun bind(info:PokeData) {

            pokemonName.text = info.pokemonName

            val imageRes = v.context.resources.getIdentifier("${info.pokemonImage}", "drawable", v.context.packageName)
            pokemonImage.setImageResource(imageRes)


        }

    }
}

My Data Class

data class PokeData(val pokemonName : String, val pokemonImage: String) {

}

Solution

  • You can have a lambda function invoked in onClick listener to update the count in the view.

    Adapter constructor can be changed something like this:

    class MainAdapter(val pokeList : List<PokeData>, val onCheckChanged: (selected: Int, total: Int) -> Unit)

    and modify onClick listener to something like this:

      holder.checkbox.setOnClickListener {
                if(holder.checkbox.isChecked) {
                    totalCollectedPokemon = totalCollectedPokemon + 1
                } else {
                    totalCollectedPokemon = totalCollectedPokemon - 1
                }
             
            onCheckChanged(totalCollectedPokemon,list.size)
            }
    

    In your fragment/activity when you initialize adapter you can handle this lambda function to update the text view.