0

I am making a data class for Parcelable. but unable to find out a solution that, how to read integer ArrayList in Parcelable class in kotlin. I know about String, but I face a problem in the Int type ArrayList.

var priceDetail: ArrayList<Int?>?,

for more detail see this,

    data class PostCardsListData(
        var arrDescriptionsTags: ArrayList<String?>?,
        var priceDetail: ArrayList<Int?>?,
..........

: Parcelable {
    constructor(parcel: Parcel) : this(
        parcel.createStringArrayList(), // i know about string
        //but how to read this Integer arraylist
        

this is my full data class code

import android.os.Parcel
import android.os.Parcelable
data class PostCardsListData(
    var arrDescriptionsTags: ArrayList<String?>?,
    var priceDetail: ArrayList<Int?>?,

) : Parcelable {
    constructor(parcel: Parcel) : this(
        parcel.createStringArrayList(),
        parcel.//describe me what i will write here

    )

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeStringList(arrDescriptionsTags)
        parcel.write//describe me what i will write here
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<PostCardsListData> {
        override fun createFromParcel(parcel: Parcel): PostCardsListData {
            return PostCardsListData(parcel)
        }

        override fun newArray(size: Int): Array<PostCardsListData?> {
            return arrayOfNulls(size)
        }
    }
}
Shirsh Shukla
  • 5,491
  • 3
  • 31
  • 44

2 Answers2

1

So to create a parcelable class.

Firstly, Add add plugin to the gradle

plugins {
    .
    .
    id 'kotlin-parcelize'
}

Then, create a class and use annotations to parcelize the data

@Parcelize
class PostCardsListData(

    @SerializedName("arrDescriptionsTags")
    var arrDescriptionsTags: ArrayList<String>? = null,

    @SerializedName("priceDetail")
    var priceDetail: ArrayList<Int?>? = null

) : Parcelable 

To read/write data you just need to create an object of this class and use the dot operator to access the list.

In the activity/fragment/viewModel class

private var listData = PostCardsListData()

//read data

private var priceList : ArrayList<Int> = ArrayList()
if(listData.priceDetail != null){
    priceList.addAll(listData.priceDetail)
}
Vatsal kesarwani
  • 664
  • 8
  • 23
0
public void writeToParcel(Parcel out, int flags) {
   out.writeList(priceDetail);
}

private PostCardsListData(Parcel in) {
   priceDetail = new ArrayList<>();
   in.readList(priceDetail, Integer.class.getClassLoader());
}

or for custom classes use

public void writeToParcel(Parcel out, int flags) {
   out.writeTypedList(listCustomClass);
}

private PostCardsListData(Parcel in) {
   listCustomClass = in.createTypedArrayList(CustomClass.CREATOR);
}
saeedata
  • 901
  • 6
  • 14
  • please take answer in kotlin, also take direct suggestion for read arraylist, like i read string arraylist parcel.createStringArrayList(), // i know about string – Shirsh Shukla Jan 13 '21 at 05:32