Когда я нажимаю элемент, я получаю
java.lang.ArrayIndexOutOfBoundsException: length = 10; индекс = -1
Это мой onCreateViewHolder
метод, когда я щелкаю элемент, я хочу открыть DetailActivity и отправлять данные с намерением
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.alphabet_item, parent, false)
val holder = ViewHolder(view)
view.setOnClickListener {
val intent = Intent(parent.context, ItemAlphabetDetailActivity::class.java)
intent.putExtra("NAME", alphabets[holder.position].context)/*
intent.putExtra("CONTEXT", alphabets[holder.adapterPosition].context)
intent.putExtra("IMAGE", alphabets[holder.adapterPosition].image)
intent.putExtra("SOUND", alphabets[holder.adapterPosition].sound)*/
parent.context.startActivity(intent)
}
return ViewHolder(view)
}
Это мой класс ItemAlphabetAdapter
class ItemAlphabetAdapter(
private val alphabets: ArrayList<ItemAlphabet>
) : RecyclerView.Adapter<ItemAlphabetAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.alphabet_item, parent, false)
val holder = ViewHolder(view)
view.setOnClickListener {
val intent = Intent(parent.context, ItemAlphabetDetailActivity::class.java)
intent.putExtra("NAME", alphabets[holder.position].context)/*
intent.putExtra("CONTEXT", alphabets[holder.adapterPosition].context)
intent.putExtra("IMAGE", alphabets[holder.adapterPosition].image)
intent.putExtra("SOUND", alphabets[holder.adapterPosition].sound)*/
parent.context.startActivity(intent)
}
return ViewHolder(view)
}
override fun getItemCount(): Int = alphabets.size
override fun onBindViewHolder(holder: ItemAlphabetAdapter.ViewHolder, position: Int) {
val alphabet = alphabets[position]
holder.name.text = alphabet.name
holder.image.visibility = alphabet.image
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val image: ImageView = itemView.findViewById(R.id.alphabet_image)
val name: TextView = itemView.findViewById(R.id.alphabet_name)
}
}