Slide 40
Slide 40 text
RecyclerView
class AnimalAdapter(val items: ArrayList) : RecyclerView.Adapter() {
// Gets the number of animals in the list
override fun getItemCount(): Int {
return items.size
}
// Inflates the item views
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.animal_list_item, parent, false))
}
// Binds each animal in the ArrayList to a view
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.tvAnimalType.text = items.get(position)
}
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
// Holds the TextView that will add each animal to
val tvAnimalType = view.tv_animal_type
}