Kotlin 适配器中的几个列表
我有一个Json和几个数组,我想在一个recyclerView中显示它们,我考虑过从适配器访问modelParent,但是没有办法 模型Kotlin 适配器中的几个列表,kotlin,adapter,android-recyclerview,Kotlin,Adapter,Android Recyclerview,我有一个Json和几个数组,我想在一个recyclerView中显示它们,我考虑过从适配器访问modelParent,但是没有办法 模型 class DetallesModelParent{ var golesLocal: ArrayList<DetLocalModel>? = null //3 items var golesVisitante: ArrayList<DetVisiModel>? = null // 2 items }
class DetallesModelParent{
var golesLocal: ArrayList<DetLocalModel>? = null //3 items
var golesVisitante: ArrayList<DetVisiModel>? = null // 2 items
}
其他
在“活动”中,所有内容都转到适配器以显示其中的所有内容:
override fun onResponse(call: Call<DetallesModelParent>, response: Response<DetallesModelParent>) {
val golLocal = (response.body()!!)
Rv_DetLocal.adapter = DetLocalAdapter(golLocal!!)
override-fun-onResponse(调用:调用,响应:响应){
val golLocal=(response.body()!!)
Rv_DetLocal.adapter=DetLocalAdapter(golLocal!!)
和适配器:
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
}
}
}
类DetLocalAdapter(val det_partido:DetallesModelParent):RecyclerView.Adapter(){
var listaLocal=ArrayList(det_partido.golesLocal)
var listaVisi=ArrayList(det_partido.golesVisitante)
override onCreateViewHolder(父级:ViewGroup,viewType:Int):ViewHolder{
val layoutInflate=LayoutInflater.from(parent.context)
.充气(右布局。partido\u goles\u行,父级,假)
返回视图保持架(平面布局)
}
覆盖BindViewHolder(holder:ViewHolder,位置:Int){
val itemDetPart=golesLocal[位置]
val itemDet=高尔夫球场[位置]
持有者。绑定项目(itemDet)
}
重写getItemCount():Int{
返回listaLocal.size+listaVisi.size
}
类ViewHolder(itemView:View):RecyclerView.ViewHolder(itemView){
趣味bindItems(plantillaModel2:DetallesModelParent){
itemView.tv\u JugadorLocal.text=plantillaModel2.golesLocal!!.get(adapterPosition).nombre
itemView.tv_JugadorVisi.text=plantillaModel2.golesVisitante!!.get(适配器位置)。nombre
}
}
}
日志:
java.lang.IndexOutOfBoundsException:索引:3,大小:3
获取(ArrayList.java:437)
位于com.myapplication.Jornadas.DetPartidos.DetLocalAdapter$ViewHolder.bindItems(DetLocalAdapter.kt:39)
位于com.myapplication.Jornadas.DetPartidos.DetLocalAdapter.onBindViewHolder(DetLocalAdapter.kt:29)
位于com.myapplication.Jornadas.DetPartidos.DetLocalAdapter.onBindViewHolder(DetLocalAdapter.kt:12)
为什么不将两个列表golesLocal和golesVisitante合并到一个哈希映射列表中
因此,IndexOutOfBoundsException将不再发生。好的,所以。您的问题是BindViewHolder上的函数
onBindViewHolder
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val itemDetPart = golesLocal[position]
val itemDet = golesVisi[position]
holder.bindItems(itemDet)
}
getItemCount
使用以下实现将返回两个列表的大小。例如,5。因此,将有以下调用:
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)
当位置达到3时,NPE将被抛出。为什么?因为在onBindViewHolder的实现中,您尝试获得…两个列表的第三个元素
因此,您需要告诉应该绑定在适配器位置上的元素
例如:
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;
}
}
override-on-bindvieholder(holder:ViewHolder,position:Int){
if(位置
数组中是否有3个项目?post 1 edited*local 3 items*visitante 2 items此调用似乎无效:det_partido[position]
det_partido is DetallesModelParent==true
,因此,您已经定义了[]DetallesModelParent
的操作员。你能把它贴在什么地方吗?你有3个golesLocal
和2个golesVisitante
。你想在列表上显示3个或2个项目吗?我想在同一个recyclerview中显示这两个列表。*右边是处所,左边是*访客,以便区分本地人和visitorswell它们将保持差异,但哈希映射使它们之间的关系协变,您可以更轻松地(稍微)管理它们我不知道该怎么做。这不是我预期的结果,它可能没有很好地解释我,但无论如何,谢谢。我想要5个项目,左边的3个场所,右边的2个访客,因此我想要2个列表。我不想要本地人和访客在同一条线上(这么说)。然后按分钟对最终列表进行排序。使用两个回收器视图。这是最简单的方法。如果是这样,我无法对它们进行排序,但还是非常感谢
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)
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;
}
}