Rafel C.F
Rafel C.F

Reputation: 179

Two arrayList in a RecyclerView

How can I show both arrayList in the same recycleview. Debug is correct with the resultado variable. How can I make the result variable show me in the RecyclerView?

In my activity DetPartido:

val resultado = (response.body()!!.golesLocal)!! + (response.body()!!.golesVisitante)!!
Rv_DetLocal.adapter = DetLocalAdapter(resultado!!)

My adapter:

    class DetLocalAdapter(val det_partido: List<Any>): RecyclerView.Adapter<DetLocalAdapter.DetallesViewHolder>() {

    private var context: Context? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DetallesViewHolder {
        val layoutInflate = LayoutInflater.from(parent.context)
            .inflate(R.layout.partido_goles_row, parent, false)
        context = parent.context

        return DetLocalAdapter.DetallesViewHolder(layoutInflate)
    }

    override fun onBindViewHolder(holder: DetallesViewHolder, position: Int) {
        val itemDetPart = det_partido[position]
        holder.bindTiempo(itemDetPart)

        holder.itemView.setOnClickListener{

            val bundle = Bundle()
            bundle.putString(Constants.ID_JUG, itemDetPart.idJugador)
            context!!.startActivity(Intent(context, DetPlantilla::class.java).putExtras(bundle))
        }
    }

    override fun getItemCount(): Int {
        return det_partido.size
    }

    class DetallesViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){
        fun bindTiempo(plantillaModel2: DetLocalModel){


            itemView.tv_JugadorLocal.text = plantillaModel2.nombreLocal + "\n Minuto: " + plantillaModel2.minutoLocal

            Picasso.get()
                .load("https://ffcv.es/ncompeticiones/img/jugadores/" + plantillaModel2.idJugador + ".jpeg")
                .fit()
                .into(itemView.civ_jugadorLocal)
        }
    }
}

My model where the arrays are DetPartModel1:

    class DetPartModel1{   

    var golesLocal: ArrayList<DetLocalModel>? = null
    var golesVisitante: ArrayList<DetVisiModel>? = null    
}

My model where they are golesLocal:

    class DetLocalModel (
    val idJugador: String,

    @SerializedName("nombre")
    val nombreLocal: String,

    @SerializedName("minuto")
    val minutoLocal: String
)

My model where they are golesVisitante:

class DetVisiModel (
    val idJugador: String,

    @SerializedName("nombre")
    val nombreVisi: String,

    @SerializedName("minuto")
    val minutoVisi: String
)

Upvotes: 0

Views: 392

Answers (1)

Hayk Melkonyan
Hayk Melkonyan

Reputation: 2150

Your models are same classes , so you can use sealed or just inheritance to differentiate them .

sealed class ModelParent(
        val idJugador: String,

        @SerializedName("nombre")
        val nombreLocal: String,

        @SerializedName("minuto")
        val minutoLocal: String
)

class DetLocalModel(id : String, number: String, minute: String)
    : ModelParent(id,number,minute)


class DetVisiModel(id : String, number: String, minute: String)
    : ModelParent(id,number,minute)



class DetPartModel1{

    var golesLocal: List<ModelParent> = mutableListOf<DetLocalModel>()
    var golesVisitante: List<ModelParent> = mutableListOf<DetVisiModel>()
}

here is how to add them to adapter

create your model:

val model:DetPartModel1 = DetPartModel1(); //todo fill correct data

fill data correctly in your model

add full list to your adapter

Rv_DetLocal.adapter = DetLocalAdapter(mutableListOf<ModelParent>().apply {
    addAll(model.golesLocal)
    addAll(model.golesVisitante)
})

here is how your adapter must look.

class DetLocalAdapter(val items: List<ModelParent>): RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        //todo
    }

    override fun getItemCount(): Int {
        items.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val item = items.get(position)
        when(item) {
            is DetVisiModel ->holder.bindDetVisiModel()
            is DetLocalModel ->holder.bindDetLocalModel()
        }
    }
}

this is fast solution , better way is to have 2 ViewHolder , 2 lists , and differentiate them by viewType

Upvotes: 1

Related Questions