Reputation: 179
Well I have a Json with several arrays that I want to show in a recyclerView, I have thought about accessing the modelParent from the adapter, but there is no way.
The Models
class DetallesModelParent{
var golesLocal: ArrayList<DetLocalModel>? = null //3 items
var golesVisitante: ArrayList<DetVisiModel>? = null // 2 items
}
Other:
class DetLocalModel(
val idJugador: String,
val nombre: String,
val minuto: String )
Other
class DetVisiModel(
val idJugador: String,
val nombre: String,
val minuto: String
)
In the activity everything went to the adapter to show everything from there:
override fun onResponse(call: Call<DetallesModelParent>, response: Response<DetallesModelParent>) {
val golLocal = (response.body()!!)
Rv_DetLocal.adapter = DetLocalAdapter(golLocal!!)
And the adapter:
class DetLocalAdapter(val det_partido: DetallesModelParent): RecyclerView.Adapter<DetLocalAdapter.ViewHolder>() {
var listaLocal = ArrayList(det_partido.golesLocal)
var listaVisi = ArrayList(det_partido.golesVisitante)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val layoutInflate = LayoutInflater.from(parent.context)
.inflate(R.layout.partido_goles_row, parent, false)
return ViewHolder(layoutInflate)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val itemDetPart = golesLocal[position]
val itemDet = golesVisi[position]
holder.bindItems(itemDet)
}
override fun getItemCount(): Int {
return listaLocal.size + listaVisi.size
}
class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
fun bindItems(plantillaModel2: DetallesModelParent){
itemView.tv_JugadorLocal.text = plantillaModel2.golesLocal!!.get(adapterPosition).nombre
itemView.tv_JugadorVisi.text = plantillaModel2.golesVisitante!!.get(adapterPosition).nombre
}
}
}
Logcat:
java.lang.IndexOutOfBoundsException: Index: 3, Size: 3 at java.util.ArrayList.get(ArrayList.java:437) at com.myapplication.Jornadas.DetPartidos.DetLocalAdapter$ViewHolder.bindItems(DetLocalAdapter.kt:39) at com.myapplication.Jornadas.DetPartidos.DetLocalAdapter.onBindViewHolder(DetLocalAdapter.kt:29) at com.myapplication.Jornadas.DetPartidos.DetLocalAdapter.onBindViewHolder(DetLocalAdapter.kt:12)
Upvotes: 0
Views: 425
Reputation: 4753
Ok, so. The problem in your case is your function onBindViewHolder
.
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val itemDetPart = golesLocal[position]
val itemDet = golesVisi[position]
holder.bindItems(itemDet)
}
getItemCount
with following implementation will return size of both lists. For example, 5. So, there will be following calls:
onBindViewHolder(holder, 0) // ok
onBindViewHolder(holder, 1) // ok
onBindViewHolder(holder, 2) // npe, as item visi has only 2 items.
onBindViewHolder(holder, 3)
onBindViewHolder(holder, 4)
When position reach 3 NPE will be thrown. Why? Because with your implementation of onBindViewHolder you try just get 3rd element of... both list.
So, you need to tell what element you should bind on adapter position
For example:
override fun onBindVieHolder(holder: ViewHolder, position: Int) {
if (position < itemDetPart.count()) {
// bind the first list
holder.bindItems(golesLocal[position])
return
} else {
// bind the second one, but remember, that param position is an adapter position, so you have to fix it.
holder.bindItems(golesVisi[position - goesLocal.count()])
return;
}
}
Upvotes: 1
Reputation:
Why doesn't merge two lists golesLocal & golesVisitante in a one hash map list so the IndexOutOfBoundsException will not occurs any more.
Upvotes: 0