When I click item I get
java.lang.ArrayIndexOutOfBoundsException: length = 10; index = -1
It's my onCreateViewHolder
method, when i click item, i want to open DetailActivity and send data with intent
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)
}
It is my class 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)
}
}
You'll have to write the following code
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)
in onBindViewHolder() which already gives you position as one of the parameters
onCreateViewHolder method is not called everytime the recyclerview needs to display a row but only when a new view needs to be created and there are not enough views on the screen. Instead, onBindViewHolder method is called for each position. So, to have the exact position of the row use holder.adapterPosition
.
Try This. Move your setOnClickListener in onBindViewHolder
itemView.setOnClickListener { if(position!=-1){
val intentIntent(itemView.context,ItemAlphabetDetailActivity::class.java)
intent.putExtra("NAME", alphabets[position].context)/*
intent.putExtra("CONTEXT", alphabets[position].context)
intent.putExtra("IMAGE", alphabets[position].image)
intent.putExtra("SOUND", alphabets[position].sound)*/
parent.context.startActivity(intent)
}}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.