从java转换到kotlin时,json模型和列表不匹配

webghufk  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(332)

无论在youtube上制作什么视频,我都会这么做,视频中使用了java语言,我试着用kotlin来做。我也一样。我试过所有的情况,但都解决不了。如果你能帮忙,我会很高兴的

//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>() {

h7wcgrx3

h7wcgrx31#

我就是这样修复的,但这次只有最后一个值出现在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()
        }
    }

相关问题