android - 将 Parcelable 对象传递给片段类型不匹配
问题描述
我是 kotlin 的新手,并试图将一个对象从我的适配器传递给一个片段。但是我在booklist
. 它说Required: Parceleable Found: List<ResourcesList>?
我也尝试过使用putParcelableArrayList
and putParcelableArray
,Serializable
但也尝试使用相同的类型不匹配。
我的数据模型如下所示:
@Parcelize
class ResourcesList (val id: Int,
val name: String,
val content: Contents,
val tags: List<Tags>) : Parcelable
@Parcelize
class Contents (val id: Int,
val producers: List<Producers>,
val categories: List<Categories>,
val isAvailable: Boolean): Parcelable
@Parcelize
class Producers (val name: String,
val role: String): Parcelable
@Parcelize
class Categories (val id: Int,
val name: String): Parcelable
分段
class SeeAllFragment: Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.see_all_layout, container, false)
val bookslist = arguments.getParceleable("bookslist")
return view
}
companion object {
fun newInstance(bookslist: List<ResourcesList>?): SeeAllFragment {
val args = Bundle()
args.putParcelable("bookslist", bookslist)
val fragment = SeeAllFragment()
fragment.arguments = args
return fragment
}
}
}
解决方案
用于将列表放入捆绑包中:
args.putParcelableArrayList("bookslist", bookslist as ArrayList<out Parcelable>?)
获取列表:
val bookslist = arguments.getParcelableArrayList<ResourcesList>("bookslist")
推荐阅读
- flutter - 如何在谷歌颤动地图上显示多个用户
- javascript - 如何通过打字稿中的setinterval增加数字
- javascript - 如何按数字键值对对象进行排序
- html - 是否可以在 div 或 span 元素上创建背景视频?
- mysql - Hybris 增加小数位数
- c++ - 如何在 C 中获取像素而不产生任何垃圾
- android - 如果删除文件AndroidManifest.xml,有办法恢复文件吗?
- asp.net-core - 使用 Gijgo 网格和 ASP.NET core 3.1 无法正确呈现网格内容
- c# - C# SQL 数据库访问
- android - 如何在命令行环境中找到 sdk.dir 位置?