Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 497
Mohamd Ali Avatar asked Dec 14 '22 15:12

Mohamd Ali


2 Answers

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>
like image 61
Montaser Sobaih Avatar answered Dec 18 '22 00:12

Montaser Sobaih


Use

  • 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)
like image 26
Abner Escócio Avatar answered Dec 18 '22 00:12

Abner Escócio