https://kotlinlang.org logo
#android
Title
# android
s

sqia09

09/13/2017, 10:31 AM
Just looking for the shortest way of implementing Parcelable and I found @Parcelize, so I need help in converting my data class into parcelable data classes:
Copy code
data class SiteResponse(@SerializedName("result") val result: String,
                        @SerializedName("freason") val freason: String,
                        @SerializedName("site") val siteList: ArrayList<SiteObject>) {

    inner class SiteObject(@SerializedName("name") val name: String,
                           @SerializedName("short_name") val shortName: String,
                           @SerializedName("id") val id: Int,
                           @SerializedName("country_id") val countryId: Int,
                           @SerializedName("region_id") val regionId: Int,
                           @SerializedName("province_id") val provinceId: Int,
                           @SerializedName("city_id") val cityId: Int,
                           @SerializedName("latitude") val latitude: String,
                           @SerializedName("longitude") val longitude: String)
}
g

gildor

09/13/2017, 10:31 AM
use backticks for your
code snippets
Copy code
triple ones in this case
As I understand Parcelaize requires to implement Parcelable in both of those classes. Also, you don’t need
inner
here. and you can make nested class data as well
So what is you problem to use Parcelize here?
s

sqia09

09/13/2017, 10:36 AM
because my own implementation is not working
g

gildor

09/13/2017, 10:36 AM
please show your implementation and error that you have
s

sqia09

09/13/2017, 10:37 AM
parcelize is the shortest way but I want to know if it has been successfully used as it's in experimental phase
Copy code
data class RoutesResponse(@SerializedName("result") val result: String,
                          @SerializedName("freason") val freason: String,
                          @SerializedName("route") var routesList: MutableList<RouteObject>,
                          @SerializedName("route_site") var routeSiteList: MutableList<RouteSite>) : Parcelable {

    companion object {
        @JvmField val CREATOR: Parcelable.Creator<RoutesResponse> = object : Parcelable.Creator<RoutesResponse>{
            override fun createFromParcel(p0: Parcel): RoutesResponse = RoutesResponse(p0)
            override fun newArray(p0: Int): Array<RoutesResponse?> = arrayOfNulls(p0)
        }
    }

    constructor(source: Parcel) : this(source.readString(),
            source.readString(),
            source.createTypedArrayList(RouteObject.CREATOR),
            source.createTypedArrayList(RouteSite.CREATOR))

    override fun describeContents() = 0

    override fun writeToParcel(p0: Parcel?, p1: Int) {
        p0?.let {
            with(it){
                writeString(result)
                writeString(freason)
                writeTypedList(routesList)
                writeTypedList(routeSiteList)
            }
        }
    }
}
g

gildor

09/13/2017, 10:38 AM
use triple backticks
s

sqia09

09/13/2017, 10:38 AM
Copy code
data class RouteObject(@SerializedName("id") val id: Int,
                       @SerializedName("name") val name: String,
                       @SerializedName("route_date") val routeDate: String,
                       @SerializedName("end_date") val endDate: String,
                       @SerializedName("created_by_id") val createdById: Int,
                       @SerializedName("team_id") val teamId: Int,
                       @SerializedName("total_total_days") val totalDays: String,
                       @SerializedName("total_site_distance") val totalSiteDistance: String,
                       @SerializedName("total_site_time") val siteTime: String,
                       @SerializedName("total_fuel_cost") val fuelCost: String,
                       @SerializedName("total_daily_allownce") val dailyAllowance: String,
                       @SerializedName("total_toll_cost") val tollCost: String,
                       @SerializedName("total_site_count") val siteCount: Int,
                       @SerializedName("route_status_id") val statusId: Int,
                       @SerializedName("teamlead_id") val teamLeadId: Int,
                       @SerializedName("created_on") val createdOn: String,
                       @SerializedName("updated_on") val updatedOn: String,
                       @SerializedName("updated_by_id") val updatedById: Int) : Parcelable {

    companion object {
        @JvmField val CREATOR: Parcelable.Creator<RouteObject> = object : Parcelable.Creator<RouteObject> {
            override fun createFromParcel(p0: Parcel): RouteObject = RouteObject(p0)
            override fun newArray(p0: Int): Array<RouteObject?> = arrayOfNulls(p0)
        }
    }

    constructor(source: Parcel) : this (source.readInt(), source.readString(), source.readString(), source.readString(), source.readInt(),
                                source.readInt(), source.readString(), source.readString(), source.readString(), source.readString(),
                                source.readString(), source.readString(), source.readInt(), source.readInt(),source.readInt(),source.readString(),
                                source.readString(), source.readInt())

    override fun describeContents(): Int = 0

    override fun writeToParcel(dest: Parcel?, flags: Int) {
        dest?.let{
            with(dest) {
                writeInt(id)
                writeString(name)
                writeString(routeDate)
                writeString(endDate)
                writeInt(createdById)
                writeInt(teamId)
                writeString(totalDays)
                writeString(totalSiteDistance)
                writeString(siteTime)
                writeString(fuelCost)
                writeString(dailyAllowance)
                writeString(tollCost)
                writeInt(siteCount)
                writeInt(statusId)
                writeInt(teamLeadId)
                writeString(createdOn)
                writeString(updatedOn)
                writeInt(updatedById)
            }
        }
    }
}
data class RouteSite( @SerializedName("id") val id: Int, @SerializedName("route_id") val routeId: Int, @SerializedName("site_id") val siteId: Int, @SerializedName("project_id") val projectId: String, @SerializedName("activity_id") val activityId: Int, @SerializedName("route_site_distance") val routeSiteDistance: String, @SerializedName("route_site_time") val routeSiteTime: String, @SerializedName("fuel_cost") val fuelCost: String, @SerializedName("daily_allownce") val dailyAllowance: String, @SerializedName("toll_cost") val tollCost: String, @SerializedName("latitude") val latitude: String, @SerializedName("longitude") val longitude: String, @SerializedName("SiteShortCode") val siteShortCode: String, @SerializedName("visit_date") val visitDate: String, @SerializedName("visit_order") val visitOrder: Int) : Parcelable { companion object { @JvmField val CREATOR : Parcelable.Creator<RouteSite> = object : Parcelable.Creator<RouteSite> { override fun createFromParcel(p0: Parcel): RouteSite = RouteSite(p0) override fun newArray(p0: Int): Array<RouteSite?> = arrayOfNulls(p0) } } constructor(source: Parcel) : this(source.readInt(), source.readInt(), source.readInt(), source.readString(), source.readInt(), source.readString(), source.readString(), source.readString(), source.readString(), source.readString(), source.readString(), source.readString(), source.readString(), source.readString(), source.readInt()) override fun describeContents(): Int = 0 override fun writeToParcel(dest: Parcel?, p1: Int) { dest?.let{ with(dest) { writeInt(id) writeInt(routeId) writeInt(siteId) writeString(projectId) writeInt(activityId) writeString(routeSiteDistance) writeString(routeSiteTime) writeString(fuelCost) writeString(dailyAllowance) writeString(tollCost) writeString(latitude) writeString(longitude) writeString(siteShortCode) writeString(visitDate) writeInt(visitOrder) } } } }
g

gildor

09/13/2017, 10:39 AM
Parcelize works, but yeah, it’s experimental, so up to you
s

sqia09

09/13/2017, 10:39 AM
I need to fix these then I'll move onto the SiteResponse and SiteObject
will it work for this kind of model in harmony with Gson SerializedName annotation?
g

gildor

09/13/2017, 10:40 AM
annotations will be just ignored
Really, I want to help, but you should figure out what kind help do you need
s

sqia09

09/13/2017, 10:41 AM
I need to get rid of that illegalstateexception: source.readString() must not be null
b

budioktaviyan

09/13/2017, 10:44 AM
If you are updating to the latest kotlin version 1.1.4-3 Parcelize already works even if you have the
annotationProcessor
inside your code. But, lint checking are still annoying ^^
g

gildor

09/13/2017, 10:47 AM
@sqia09 I already pointed you in ahother thread that probably you use deserialization of List in wrong way. Your Parcelable looks good, so it’s not a problem in implementation of Parcelable itself
s

sqia09

09/13/2017, 10:47 AM
ok then I'm retrieving the array the wrong way...
i'll try something else
g

gildor

09/13/2017, 10:48 AM
You can sure that your parcelable is correct of you send only one object instead of list
Also, you showed my example of another class, that has two list inside, if you want to send RouteSite list it’s easy, just create an Intent and put your list:
Intent().putParcelableArrayListExtra(ArrayList(listOfRouteSite))
then use getParcelableArrayListExtra()
but if you have more complex example,. it’s just hard to me figureout what’s going on there
s

sqia09

09/13/2017, 10:54 AM
ok thanks.
thanks solved it...i had to give default values for Ints and added ? in string fields.
It looks like I was getting null for some fields from the webservice
and used the above mentioned code for passing the array
thanks again!
2 Views