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

How to get the data from an getParcelableArrayListExtra in kotlin?.

I have a Parceable data class called FoodParceable

data class FoodParceable(val idCover: Int, val name: String, val 
price: Double):Parcelable {
constructor(parcel: Parcel) : this(
    parcel.readInt(),
    parcel.readString(),
    parcel.readDouble()
override fun writeToParcel(parcel: Parcel, flags: Int) {
    parcel.writeInt(idCover)
    parcel.writeString(name)
    parcel.writeDouble(price)
override fun describeContents(): Int {
    return 0
companion object CREATOR : Parcelable.Creator<FoodParceable> {
    override fun createFromParcel(parcel: Parcel): FoodParceable {
        return FoodParceable(parcel)
    override fun newArray(size: Int): Array<FoodParceable?> {
        return arrayOfNulls(size)

In the current Acitvity A, fill in the arrayListParceable and send it to activity B.

    val arrayListParceable = ArrayList<FoodParceable>()
    for (Food in listFood) {
        arrayListParceable.add(FoodParceable(R.mipmap.ic_food_meat, "Carne", 9.99))
        arrayListParceable.add(FoodParceable(R.mipmap.ic_food_meat, "Vegetales", 29.99))
        arrayListParceable.add(FoodParceable(R.mipmap.ic_food_meat, "Frutas", 39.99))
    val intent = Intent()
    intent.putParcelableArrayListExtra(LIST_PRODUCT,arrayListParceable)
    activity?.setResult(Activity.RESULT_OK, intent)

When I get the value in activity B, in list item, I can not enter its content.

if(resultCode == Activity.RESULT_OK){
                if(data != null){
                    val listItems = data.getParcelableArrayListExtra<Parcelable>(LIST_PRODUCT)
                    listItems[0]. //ERROR

I am new to kotlin, thank you for your comments. Regards

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.