在Adapter类中获取ArrayIndexOutOfBoundsException

时间:2019-01-10 09:37:31

标签: java android kotlin

当我单击项目时,我会得到

  

java.lang.ArrayIndexOutOfBoundsException:长度= 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)

}

}

3 个答案:

答案 0 :(得分:0)

您必须编写以下代码

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)

在onBindViewHolder()中,它已经为您提供了位置作为参数之一

答案 1 :(得分:0)

onCreateViewHolder方法不会在每次recyclerview需要显示一行时调用,而仅在需要创建新视图且屏幕上没有足够视图时才调用。而是,为每个位置调用onBindViewHolder方法。因此,要获得该行的确切位置,请使用$ git checkout -b estimated-time Switched to a new branch 'estimated-time' D ~$W error messages.docx

答案 2 :(得分:0)

尝试一下。将您的setOnClickListener移动到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)
}}