Afdal
Afdal

Reputation: 655

How to create constructor for data class in Kotlin Android?

How to create constructor for data class in Kotlin Android ?

data class EventItem(
        @SerializedName("dateEvent")
        val dateEvent: String,
        @SerializedName("dateEventLocal")
        val dateEventLocal: Any,
        @SerializedName("idAwayTeam")
        val idAwayTeam: String,
        @SerializedName("idEvent")
        val idEvent: String,
        @SerializedName("idHomeTeam")
        val idHomeTeam: String,
        @SerializedName("idLeague")
        val idLeague: String,
        @SerializedName("idSoccerXML")
        val idSoccerXML: String,
        @SerializedName("intAwayScore")
        val intAwayScore: Any,
        @SerializedName("intAwayShots")
        val intAwayShots: Any,
        @SerializedName("intHomeScore")
        val intHomeScore: Any,
        @SerializedName("intHomeShots")
        val intHomeShots: Any,
        @SerializedName("intRound")
        val intRound: String,
        @SerializedName("intSpectators")
        val intSpectators: Any,
        @SerializedName("strAwayFormation")
        val strAwayFormation: Any,
        @SerializedName("strAwayGoalDetails")
        val strAwayGoalDetails: String,
        @SerializedName("strAwayLineupDefense")
        val strAwayLineupDefense: String,
        @SerializedName("strAwayLineupForward")
        val strAwayLineupForward: String,
        @SerializedName("strAwayLineupGoalkeeper")
        val strAwayLineupGoalkeeper: String,
        @SerializedName("strAwayLineupMidfield")
        val strAwayLineupMidfield: String,
        @SerializedName("strAwayLineupSubstitutes")
        val strAwayLineupSubstitutes: String,
        @SerializedName("strAwayRedCards")
        val strAwayRedCards: String,
        @SerializedName("strAwayTeam")
        val strAwayTeam: String,
        @SerializedName("strAwayYellowCards")
        val strAwayYellowCards: String,
        @SerializedName("strBanner")
        val strBanner: Any,
        @SerializedName("strCircuit")
        val strCircuit: Any,
        @SerializedName("strCity")
        val strCity: Any,
        @SerializedName("strCountry")
        val strCountry: Any,
        @SerializedName("strDate")
        val strDate: String,
        @SerializedName("strDescriptionEN")
        val strDescriptionEN: Any,
        @SerializedName("strEvent")
        val strEvent: String,
        @SerializedName("strEventAlternate")
        val strEventAlternate: String,
        @SerializedName("strFanart")
        val strFanart: Any,
        @SerializedName("strFilename")
        val strFilename: String,
        @SerializedName("strHomeFormation")
        val strHomeFormation: Any,
        @SerializedName("strHomeGoalDetails")
        val strHomeGoalDetails: String,
        @SerializedName("strHomeLineupDefense")
        val strHomeLineupDefense: String,
        @SerializedName("strHomeLineupForward")
        val strHomeLineupForward: String,
        @SerializedName("strHomeLineupGoalkeeper")
        val strHomeLineupGoalkeeper: String,
        @SerializedName("strHomeLineupMidfield")
        val strHomeLineupMidfield: String,
        @SerializedName("strHomeLineupSubstitutes")
        val strHomeLineupSubstitutes: String,
        @SerializedName("strHomeRedCards")
        val strHomeRedCards: String,
        @SerializedName("strHomeTeam")
        val strHomeTeam: String,
        @SerializedName("strHomeYellowCards")
        val strHomeYellowCards: String,
        @SerializedName("strLeague")
        val strLeague: String,
        @SerializedName("strLocked")
        val strLocked: String,
        @SerializedName("strMap")
        val strMap: Any,
        @SerializedName("strPoster")
        val strPoster: Any,
        @SerializedName("strResult")
        val strResult: Any,
        @SerializedName("strSeason")
        val strSeason: String,
        @SerializedName("strSport")
        val strSport: String,
        @SerializedName("strTVStation")
        val strTVStation: Any,
        @SerializedName("strThumb")
        val strThumb: Any,
        @SerializedName("strTime")
        val strTime: String,
        @SerializedName("strTimeLocal")
        val strTimeLocal: String,
        @SerializedName("strTweet1")
        val strTweet1: Any,
        @SerializedName("strTweet2")
        val strTweet2: Any,
        @SerializedName("strTweet3")
        val strTweet3: Any,
        @SerializedName("strVideo")
        val strVideo: Any
    ) {
        constructor(
            idEvent: String,
            strEvent: String,
            strDate: String,
            idHomeTeam: String,
            strHomeTeam: String,
            intHomeScore: Any,
            idAwayTeam: String,
            strAwayTeam: String,
            intAwayScore: Any
            ) : this(idEvent, strEvent, strDate, idHomeTeam, strHomeTeam,  intHomeScore, idAwayTeam, strAwayTeam, intAwayScore)
    }

screenshot : https://i.sstatic.net/riGkU.png

How to create constructor for data class in Kotlin Android ?

i've try to create constructor. But, i get "There's a cycle in the delegation calls chain" please, correct my code and tell me solution for it. . .

Upvotes: 1

Views: 3602

Answers (1)

Ben P.
Ben P.

Reputation: 54194

For a data class, the constructor defined in the class header is the primary constructor. It can't delegate to any other constructor in that class (though it can delegate to a superclass constructor).

However, you can define secondary constructors in the body of the class, as long as those delegate to the primary constructor. For example:

data class EventItem(val dateEvent: String) {

    constructor(date: Date) : this(date.toString())
}

The problem in the code you've posted is that your secondary constructor is trying to delegate back to itself. You have to delegate to the primary constructor instead, and that means you need to be able to determine a value for all of the parameters missing from the secondary constructor.

Upvotes: 2

Related Questions