Skip to content
Advertisement

Model and list do not match when converting from java to kotlin

Whatever a video is made on Youtube, I do the same, Java language is used in the video, and I try to do it with Kotlin. I do exactly the same. I tried all the situations but could not fix them. I would be glad if you help

//MainActiviy
var photoList: List<Photo>? = null
...
photoList = ArrayList()
...
photoList = ArrayList(Arrays.asList(demoModel!!.photos))

//Model
data class DemoModel(
    @SerializedName("photos")
    var photos: List<Photo>? = null
)

//Adapter
class Adapter(private val context: Context, private val photoList: List<Photo>) :
    RecyclerView.Adapter<Adapter.MyViewHolder>() {

enter image description here

Advertisement

Answer

This is how I fixed it, but this time only the last value appears in the recyclerView @aldrin

//In call;
val demoModel = response.body()
                demoModel?.let {
                    PutDataIntoRecyclerView(ArrayList(Arrays.asList(it.photos)))
                }


//In recyclerView function;
fun PutDataIntoRecyclerView(photoList: List<List<Photo>?>) {
        val adapter = Adapter(this, photoList)
        binding.recyclerViewID.setLayoutManager(LinearLayoutManager(this))
        binding.recyclerViewID.adapter = adapter
    }

//In Adapter
class Adapter(private val context: Context, private val photoList: List<List<Photo>?>) :
    RecyclerView.Adapter<Adapter.MyViewHolder>() {
....
//In Adapter onBindViewHolder;
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val myList = photoList[position]
        myList?.forEach {
            println(it.imgSrc)
            holder.photoId?.text = it.id.toString()
            holder.photoSol?.text = it.sol.toString()
            holder.photoEarthDate?.text = it.earthDate.toString()
        }
    }
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement