本文介绍了将可分割对象传递给片段类型不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是kotlin的新手,正在尝试将对象从适配器传递给片段.但是我在booklist的伴随对象中遇到类型不匹配的情况.它说Required: Parceleable Found: List<ResourcesList>?

I'm new to kotlin and trying to pass an object from my adapter to a fragment. But I am getting a type mismatch in the companion object for booklist. It says Required: Parceleable Found: List<ResourcesList>?

我也尝试过使用putParcelableArrayListputParcelableArraySerializable,但是类型不匹配.

I've also tried using putParcelableArrayList and putParcelableArray and Serializable but also with the same type mismatch.

我的数据模型如下:

@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")

这篇关于将可分割对象传递给片段类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 07:01