Lemonyara
Lemonyara

Reputation: 53

How to make parcelable class with nested classes

I'm new in Android dev. And I was faced with the fact that I need to redo my class into Parcelable class. And the main problem with it is that i have 3 nested classes into my class.

It is my code:

class Test      (var id: Int,
                 var numberQuestion: String,
                 var question: String,
                 var questionImageSrc: String,
                 var examination: Boolean,
                 var typeQuestion: String,
                 var singleChoiceAnswers: ArrayList<singleChoiceAnswer>,
                 var multipleChoiceAnswers: ArrayList<multipleChoiceAnswers>,
                 var inputAnswer: ArrayList<inputAnswer>): Parcelable{

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

        // ????

        )

    companion object{
        @JvmField
        val CREATOR = object: Parcelable.Creator<Test>{
            override fun newArray(size: Int) = arrayOfNulls<Test>(size)
            override fun createFromParcel(source: Parcel?): Test {
                return Test(source!!)
            }
        }
    }

    override fun writeToParcel(dest: Parcel?, flags: Int) {
        dest?.writeInt(id)
        dest?.writeString(numberQuestion)
        dest?.writeString(question)
        dest?.writeString(questionImageSrc)
        dest?.writeValue(examination)
        dest?.writeString(typeQuestion)
        dest?.
    }

    override fun describeContents(): Int {
        TODO("Not yet implemented")
    }

}

data class multipleChoiceAnswers(var letter: String,
                                 var text: String,
                                 var correctAnswer: Boolean,
                                 var checked: Boolean)

data class singleChoiceAnswer(var letter: String,
                              var text: String,
                              var correctAnswer: Boolean,
                              var checked: Boolean)

data class inputAnswer(var correctAnswer: String,
                        var userAnswer: String)

I need to make nested classes Parcelable too ? And i take data from JSON. If you need here is a link

Thank you in advance) P.S. Sorry for my English

Upvotes: 0

Views: 1339

Answers (1)

Charly Lafon
Charly Lafon

Reputation: 589

No need to do all that. Just use @Parcelize from Kotlin. Annotate all theses classes with @Parcelize and make them extend Parcelable. You can remove all your Parcelable related code in the Test class.

Also I'll recommend modifications for a few things wrong that I see in your code :

  • Turn all your var properties to val. It will enforce immutability, one of Kotlin's basic features. The same goes for your ArrayLists, use List instead to disallow modifications of the list. If you really can't have a read-only List then use MutableList. It's more easy to understand that it's a read/write list than when you use ArrayList.
  • Make your Test class a data class. I don't see why not benefit from it in your case.
  • All classes name must begin with an uppercase. multipleChoiceAnswers -> MultipleChoiceAnswers. It's a convention we follow in many languages, not just Kotlin.

Upvotes: 3

Related Questions