0

in my custom object, i have 7 attributes, 5 of them are strings, and were auto genereated in the contructor fine, but the other two did not generate automatically. the last two are of class Place and ArrayList<int>:

class Spot() : Parcelable{
    private var uid: String? = null
    private var timeFrom: String? = null
    private var timeTo: String? = null
    private var rate: String? = null
    private var description: String? = null
    private var place: Place? = null
    private var days : ArrayList<Int>? = null

    constructor(parcel: Parcel) : this() {
        uid = parcel.readString()
        timeFrom = parcel.readString()
        timeTo = parcel.readString()
        rate = parcel.readString()
        description = parcel.readString()
    }
...

How do i parcelize them?

Barcode
  • 930
  • 1
  • 13
  • 31

1 Answers1

0

There are several methods to do it. You need to add the code yourself for ArrayList and Place Object in the constructor and writeToParcel methods.

constructor

constructor(parcel: Parcel) : this() {
        uid = parcel.readString()
        timeFrom = parcel.readString()
        timeTo = parcel.readString()
        rate = parcel.readString()
        description = parcel.readString()

        // Add the below to line of code
        days = parcel.readArrayList(Int::class.java.classLoader) as ArrayList<Int>?
        place = parcel.readParcelable<Place>(Place::class.java.classLoader)
    }

writeToParcel

override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(uid)
        parcel.writeString(timeFrom)
        parcel.writeString(timeTo)
        parcel.writeString(rate)
        parcel.writeString(description)

        // Add the below to line of code
        parcel.writeList(days)
        parcel.writeParcelable(place, flags)

    }

And if I'm not wrong, Googles Place class is already Parcelable so this code should work.

Hope it helps!

bhavya_karia
  • 760
  • 1
  • 6
  • 12
  • `.readParcelable` gives error: `Type argument is not within its bounds, Expected Parcelable!, Found Place` – Barcode Mar 08 '19 at 17:09
  • which Place object are you using? It's saying the Place class is not Parcelable. I have used [this](https://developers.google.com/places/android-sdk/reference/com/google/android/libraries/places/api/model/Place) Google Place Object. – bhavya_karia Mar 09 '19 at 05:12