添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Android kotlin: putParcelableArrayList(ArrayList<int>) and getParcelableArrayList<Int>() wont work

Ask Question

I am trying to pass ArrayList<Integer> from fragment to another fragment, here is my code:

Kotlin code

companion object {
    fun newInstance(categoryId: Int, brandsList: ArrayList<Int>): Fragment {
        val fragment = CategoryAllAdsFragment()
        fragment.arguments = Bundle()
        fragment.arguments!!.putInt(Constant.CATEGORY_ID, categoryId)
        fragment.arguments!!.putParcelableArrayList(Constant.BRANDS_LIST, brandsList)
        return fragment

but it says:

Type mismatch.

Required: java.util.ArrayList !

Found: kotlin.collections.ArrayList /* = java.util.ArrayList */

The same thing when I was trying to read it.

Kotlin code

try {
    val brandsList = arguments!!.getParcelableArrayList<Int>(Constant.BRANDS_LIST)
} catch (ex: Exception) {
    throw Exception("brand list cannot be null")

It says:

Type argument is not within its bounds

Expected: Parcelable!

Found: Int

I've tested it with Java and its work fine.

You can make a general Parcelable class that contains a variable of type (Any)

class BaseParcelable : Parcelable {
    var value: Any
    constructor(value: Any) {
        this.value = value
    constructor(parcel: Parcel) {
        this.value = Any()
    override fun writeToParcel(dest: Parcel?, flags: Int) {}
    override fun describeContents(): Int = 0
    companion object CREATOR : Parcelable.Creator<BaseParcelable> {
        override fun createFromParcel(parcel: Parcel): BaseParcelable {
            return BaseParcelable(parcel)
        override fun newArray(size: Int): Array<BaseParcelable?> {
            return arrayOfNulls(size)

Then use this class to pass data between fragments or between (Activty to Fragment)

To pass list from fragment to fragment for example:

companion object {
    fun newInstance(categoryId: Int, brandsList: ArrayList<Int>): Fragment {
       val fragment = CategoryAllAdsFragment()
       fragment.arguments = Bundle()
       fragment.arguments!!.putInt(Constant.CATEGORY_ID, categoryId)
       fragment.arguments!!.putParcelable(Constant.BRANDS_LIST, BaseParcelable(brandsList))
       return fragment

To get the list:

val any = arguments?.getParcelable<BaseParcelable>(Constant.BRANDS_LIST).value
val list = any as ArrayList<Int>
                Caused by: java.lang.ClassCastException: java.lang.Object cannot be cast to java.util.ArrayList
– Ahamadullah Saikat
                Jun 20, 2021 at 12:00
  
  • putIntegerArrayList(String key, ArrayList value)

  • Inserts an ArrayList value into the mapping of this Bundle, replacing any existing value for the given key

  • putIntegerArrayList(Constant.BRANDS_LIST, array)
    

    And get like

  • getIntegerArrayList(String key)

  • Returns the value associated with the given key, or null if no mapping of the desired type exists for the given key or a null value is explicitly associated with the key.

  • extras.getIntegerArrayList(Constant.BRANDS_LIST)
                    the extras.getIntArray(Constant.BRANDS_LIST) maybe will work, but there is no putExtra(Constant.BRANDS_LIST, array) in Bundle
    – Mohamd Ali
                    Jul 13, 2018 at 19:36
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.