Complete the code to create a RecyclerView adapter class in Kotlin.
class MyAdapter(private val items: List<String>) : RecyclerView.[1]<MyAdapter.ViewHolder>() { // Adapter implementation }
The adapter class must extend RecyclerView.Adapter to work with RecyclerView.
Complete the code to inflate the item layout in onCreateViewHolder method.
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate([1], parent, false)
return ViewHolder(view)
}LayoutInflater.inflate requires a layout resource ID, which is R.layout.item_view.
Fix the error in binding data to the TextView inside onBindViewHolder.
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.textView.[1] = items[position]
}In Kotlin Android extensions, you set the text of a TextView by assigning to its text property.
Fill both blanks to create a list that updates efficiently with RecyclerView.
val recyclerView = findViewById<RecyclerView>(R.id.recyclerView) recyclerView.layoutManager = [1](this) recyclerView.adapter = MyAdapter([2])
RecyclerView needs a LayoutManager like LinearLayoutManager to arrange items vertically. The adapter needs a list, and arrayListOf is a mutable list suitable for dynamic updates.
Fill all three blanks to implement ViewHolder class with a TextView reference.
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { val [1]: TextView = itemView.findViewById([2]) fun bind(text: String) { [3].text = text } }
The ViewHolder holds a reference to the TextView named textView. It finds it by R.id.textView. The bind function sets the text on this textView.