gpt4 book ai didi

kotlin - 访问对象Kotlin中的元素

转载 作者:行者123 更新时间:2023-12-02 13:10:44 25 4
gpt4 key购买 nike

我正在尝试访问对象内的各个元素,然后将其显示为 View 的一部分。目标是将每个元素分配给对象外部的变量。
我的适配器

private class MyCustomAdapter(list: List<Object>?, totalCount: Int): RecyclerView.Adapter<CustomViewHolder>() {
private val listings = list;
private val totalCount = totalCount;

override fun getItemCount(): Int {
if (listings != null) {
return listings.size
}
return 0
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
val layoutInflater = LayoutInflater.from(parent.context);
val cellForRow = layoutInflater.inflate(R.layout.auction_main, parent, false);
return CustomViewHolder(cellForRow);
}

override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
val obj = listings?.get(position);
holder.view.textView_auction_title?.text = obj.toString()
}

}
listings?.get(position)看起来像这样:
     {
id = 2,
title = "testtitle1",
category = pants,
}
但是,我试图将ID,标题和类别显示为单个元素,而不是将整个对象显示为文本。我对Kotlin还是很陌生,因此将不胜感激。

最佳答案

您不应该使用List<Object>
如果所有数据列表对象都遵循相同的id,title,category结构,则应该使用该列表的类来构建模型。
如果该列表中的数据类型不同,则可以使用List<Any>并执行类似if (obj is IdTitleCategoryModel) view.text = obj.title的操作,也可以使用List<JSONObject>(我永远不会建议针对特定类型的模型使用)

关于kotlin - 访问对象Kotlin中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63294320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com