package com.example.myapplication.keyboard

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.myapplication.R

class SimpleStringGridAdapter(
    private val itemLayoutRes: Int,
    private val onClick: (String) -> Unit
) : RecyclerView.Adapter<SimpleStringGridAdapter.VH>() {

    private val data = ArrayList<String>()

    fun submit(list: List<String>) {
        data.clear()
        data.addAll(list)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
        val tv = LayoutInflater.from(parent.context)
            .inflate(itemLayoutRes, parent, false) as TextView
        return VH(tv)
    }

    override fun onBindViewHolder(holder: VH, position: Int) {
        val s = data[position]
        holder.tv.text = s
        holder.tv.setOnClickListener { onClick(s) }
    }

    override fun getItemCount(): Int = data.size

    class VH(val tv: TextView) : RecyclerView.ViewHolder(tv)
}

