How to add setter and getter to member variables of a Parcelable class












0















Below is a class that I'm currently working on



@Parcelize
class Time(var hour:Int, var minute:Int):Comparable<Time>,Parcelable


I want to add a setter to member variables hour and minute so that they would stay in time format. And the class has to be passed through intent, so it implements Parcelable. But it says that a Parcelable class should have a primary constructor so I'm having problem with adding setters.
Can anyone please tell me why and a solution? Thank you.










share|improve this question

























  • make it a data class

    – Tim Castelijns
    Nov 27 '18 at 12:29











  • I'm not familiar with kotlin, but what exactly hinders you writing custom methods?

    – Rafael T
    Nov 27 '18 at 12:37











  • @RafaelT Custom methods for what?

    – BullGom
    Nov 27 '18 at 12:48
















0















Below is a class that I'm currently working on



@Parcelize
class Time(var hour:Int, var minute:Int):Comparable<Time>,Parcelable


I want to add a setter to member variables hour and minute so that they would stay in time format. And the class has to be passed through intent, so it implements Parcelable. But it says that a Parcelable class should have a primary constructor so I'm having problem with adding setters.
Can anyone please tell me why and a solution? Thank you.










share|improve this question

























  • make it a data class

    – Tim Castelijns
    Nov 27 '18 at 12:29











  • I'm not familiar with kotlin, but what exactly hinders you writing custom methods?

    – Rafael T
    Nov 27 '18 at 12:37











  • @RafaelT Custom methods for what?

    – BullGom
    Nov 27 '18 at 12:48














0












0








0








Below is a class that I'm currently working on



@Parcelize
class Time(var hour:Int, var minute:Int):Comparable<Time>,Parcelable


I want to add a setter to member variables hour and minute so that they would stay in time format. And the class has to be passed through intent, so it implements Parcelable. But it says that a Parcelable class should have a primary constructor so I'm having problem with adding setters.
Can anyone please tell me why and a solution? Thank you.










share|improve this question
















Below is a class that I'm currently working on



@Parcelize
class Time(var hour:Int, var minute:Int):Comparable<Time>,Parcelable


I want to add a setter to member variables hour and minute so that they would stay in time format. And the class has to be passed through intent, so it implements Parcelable. But it says that a Parcelable class should have a primary constructor so I'm having problem with adding setters.
Can anyone please tell me why and a solution? Thank you.







android kotlin






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 12:54







BullGom

















asked Nov 27 '18 at 12:29









BullGomBullGom

11812




11812













  • make it a data class

    – Tim Castelijns
    Nov 27 '18 at 12:29











  • I'm not familiar with kotlin, but what exactly hinders you writing custom methods?

    – Rafael T
    Nov 27 '18 at 12:37











  • @RafaelT Custom methods for what?

    – BullGom
    Nov 27 '18 at 12:48



















  • make it a data class

    – Tim Castelijns
    Nov 27 '18 at 12:29











  • I'm not familiar with kotlin, but what exactly hinders you writing custom methods?

    – Rafael T
    Nov 27 '18 at 12:37











  • @RafaelT Custom methods for what?

    – BullGom
    Nov 27 '18 at 12:48

















make it a data class

– Tim Castelijns
Nov 27 '18 at 12:29





make it a data class

– Tim Castelijns
Nov 27 '18 at 12:29













I'm not familiar with kotlin, but what exactly hinders you writing custom methods?

– Rafael T
Nov 27 '18 at 12:37





I'm not familiar with kotlin, but what exactly hinders you writing custom methods?

– Rafael T
Nov 27 '18 at 12:37













@RafaelT Custom methods for what?

– BullGom
Nov 27 '18 at 12:48





@RafaelT Custom methods for what?

– BullGom
Nov 27 '18 at 12:48












1 Answer
1






active

oldest

votes


















2














Kotlin's properties are already shipped with getters and setters. Every time you say time.hour = 5 or val currentTime = time.hour - corresponding setter or getter is invoked.



The problem here is not with constructor, but with CREATOR that parcelable requires.



The complete example would look like this:



class Time(var hour: Int, var minute: Int) : Comparable<Time>, Parcelable {

override fun writeToParcel(dest: Parcel?, flags: Int) {
dest!!.writeInt(hour)
dest.writeInt(minute)
}

override fun describeContents(): Int = 0

override fun compareTo(other: Time): Int {
// your comparsion logic
}

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

override fun newArray(size: Int): Array<Time?> {
return arrayOfNulls(size)
}
}
}





share|improve this answer


























  • I'm currently using the @Parcelize decorator. Should I override CREATOR?

    – BullGom
    Nov 27 '18 at 12:51











  • If you use @Parcelize, then making this class a data class (data class Time(var hour: Int, var minute: Int) : Comparable<Time>, Parcelable) would be enough.

    – Andrey Ilyunin
    Nov 27 '18 at 12:53











  • How do you modify setter?

    – BullGom
    Nov 27 '18 at 12:54











  • You mean overriding default setter with custom implementation? Here's how you can do it: stackoverflow.com/questions/38492103/…

    – Andrey Ilyunin
    Nov 27 '18 at 12:58








  • 1





    It is not recommended to contain processing logic inside data classes, so I would recommend either to wrap Time data class instance with another object that knows how to parse, or making Time not data class and implement mentioned methods yourself.

    – Andrey Ilyunin
    Nov 27 '18 at 13:01











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53499729%2fhow-to-add-setter-and-getter-to-member-variables-of-a-parcelable-class%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Kotlin's properties are already shipped with getters and setters. Every time you say time.hour = 5 or val currentTime = time.hour - corresponding setter or getter is invoked.



The problem here is not with constructor, but with CREATOR that parcelable requires.



The complete example would look like this:



class Time(var hour: Int, var minute: Int) : Comparable<Time>, Parcelable {

override fun writeToParcel(dest: Parcel?, flags: Int) {
dest!!.writeInt(hour)
dest.writeInt(minute)
}

override fun describeContents(): Int = 0

override fun compareTo(other: Time): Int {
// your comparsion logic
}

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

override fun newArray(size: Int): Array<Time?> {
return arrayOfNulls(size)
}
}
}





share|improve this answer


























  • I'm currently using the @Parcelize decorator. Should I override CREATOR?

    – BullGom
    Nov 27 '18 at 12:51











  • If you use @Parcelize, then making this class a data class (data class Time(var hour: Int, var minute: Int) : Comparable<Time>, Parcelable) would be enough.

    – Andrey Ilyunin
    Nov 27 '18 at 12:53











  • How do you modify setter?

    – BullGom
    Nov 27 '18 at 12:54











  • You mean overriding default setter with custom implementation? Here's how you can do it: stackoverflow.com/questions/38492103/…

    – Andrey Ilyunin
    Nov 27 '18 at 12:58








  • 1





    It is not recommended to contain processing logic inside data classes, so I would recommend either to wrap Time data class instance with another object that knows how to parse, or making Time not data class and implement mentioned methods yourself.

    – Andrey Ilyunin
    Nov 27 '18 at 13:01
















2














Kotlin's properties are already shipped with getters and setters. Every time you say time.hour = 5 or val currentTime = time.hour - corresponding setter or getter is invoked.



The problem here is not with constructor, but with CREATOR that parcelable requires.



The complete example would look like this:



class Time(var hour: Int, var minute: Int) : Comparable<Time>, Parcelable {

override fun writeToParcel(dest: Parcel?, flags: Int) {
dest!!.writeInt(hour)
dest.writeInt(minute)
}

override fun describeContents(): Int = 0

override fun compareTo(other: Time): Int {
// your comparsion logic
}

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

override fun newArray(size: Int): Array<Time?> {
return arrayOfNulls(size)
}
}
}





share|improve this answer


























  • I'm currently using the @Parcelize decorator. Should I override CREATOR?

    – BullGom
    Nov 27 '18 at 12:51











  • If you use @Parcelize, then making this class a data class (data class Time(var hour: Int, var minute: Int) : Comparable<Time>, Parcelable) would be enough.

    – Andrey Ilyunin
    Nov 27 '18 at 12:53











  • How do you modify setter?

    – BullGom
    Nov 27 '18 at 12:54











  • You mean overriding default setter with custom implementation? Here's how you can do it: stackoverflow.com/questions/38492103/…

    – Andrey Ilyunin
    Nov 27 '18 at 12:58








  • 1





    It is not recommended to contain processing logic inside data classes, so I would recommend either to wrap Time data class instance with another object that knows how to parse, or making Time not data class and implement mentioned methods yourself.

    – Andrey Ilyunin
    Nov 27 '18 at 13:01














2












2








2







Kotlin's properties are already shipped with getters and setters. Every time you say time.hour = 5 or val currentTime = time.hour - corresponding setter or getter is invoked.



The problem here is not with constructor, but with CREATOR that parcelable requires.



The complete example would look like this:



class Time(var hour: Int, var minute: Int) : Comparable<Time>, Parcelable {

override fun writeToParcel(dest: Parcel?, flags: Int) {
dest!!.writeInt(hour)
dest.writeInt(minute)
}

override fun describeContents(): Int = 0

override fun compareTo(other: Time): Int {
// your comparsion logic
}

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

override fun newArray(size: Int): Array<Time?> {
return arrayOfNulls(size)
}
}
}





share|improve this answer















Kotlin's properties are already shipped with getters and setters. Every time you say time.hour = 5 or val currentTime = time.hour - corresponding setter or getter is invoked.



The problem here is not with constructor, but with CREATOR that parcelable requires.



The complete example would look like this:



class Time(var hour: Int, var minute: Int) : Comparable<Time>, Parcelable {

override fun writeToParcel(dest: Parcel?, flags: Int) {
dest!!.writeInt(hour)
dest.writeInt(minute)
}

override fun describeContents(): Int = 0

override fun compareTo(other: Time): Int {
// your comparsion logic
}

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

override fun newArray(size: Int): Array<Time?> {
return arrayOfNulls(size)
}
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 27 '18 at 12:51

























answered Nov 27 '18 at 12:45









Andrey IlyuninAndrey Ilyunin

1,318220




1,318220













  • I'm currently using the @Parcelize decorator. Should I override CREATOR?

    – BullGom
    Nov 27 '18 at 12:51











  • If you use @Parcelize, then making this class a data class (data class Time(var hour: Int, var minute: Int) : Comparable<Time>, Parcelable) would be enough.

    – Andrey Ilyunin
    Nov 27 '18 at 12:53











  • How do you modify setter?

    – BullGom
    Nov 27 '18 at 12:54











  • You mean overriding default setter with custom implementation? Here's how you can do it: stackoverflow.com/questions/38492103/…

    – Andrey Ilyunin
    Nov 27 '18 at 12:58








  • 1





    It is not recommended to contain processing logic inside data classes, so I would recommend either to wrap Time data class instance with another object that knows how to parse, or making Time not data class and implement mentioned methods yourself.

    – Andrey Ilyunin
    Nov 27 '18 at 13:01



















  • I'm currently using the @Parcelize decorator. Should I override CREATOR?

    – BullGom
    Nov 27 '18 at 12:51











  • If you use @Parcelize, then making this class a data class (data class Time(var hour: Int, var minute: Int) : Comparable<Time>, Parcelable) would be enough.

    – Andrey Ilyunin
    Nov 27 '18 at 12:53











  • How do you modify setter?

    – BullGom
    Nov 27 '18 at 12:54











  • You mean overriding default setter with custom implementation? Here's how you can do it: stackoverflow.com/questions/38492103/…

    – Andrey Ilyunin
    Nov 27 '18 at 12:58








  • 1





    It is not recommended to contain processing logic inside data classes, so I would recommend either to wrap Time data class instance with another object that knows how to parse, or making Time not data class and implement mentioned methods yourself.

    – Andrey Ilyunin
    Nov 27 '18 at 13:01

















I'm currently using the @Parcelize decorator. Should I override CREATOR?

– BullGom
Nov 27 '18 at 12:51





I'm currently using the @Parcelize decorator. Should I override CREATOR?

– BullGom
Nov 27 '18 at 12:51













If you use @Parcelize, then making this class a data class (data class Time(var hour: Int, var minute: Int) : Comparable<Time>, Parcelable) would be enough.

– Andrey Ilyunin
Nov 27 '18 at 12:53





If you use @Parcelize, then making this class a data class (data class Time(var hour: Int, var minute: Int) : Comparable<Time>, Parcelable) would be enough.

– Andrey Ilyunin
Nov 27 '18 at 12:53













How do you modify setter?

– BullGom
Nov 27 '18 at 12:54





How do you modify setter?

– BullGom
Nov 27 '18 at 12:54













You mean overriding default setter with custom implementation? Here's how you can do it: stackoverflow.com/questions/38492103/…

– Andrey Ilyunin
Nov 27 '18 at 12:58







You mean overriding default setter with custom implementation? Here's how you can do it: stackoverflow.com/questions/38492103/…

– Andrey Ilyunin
Nov 27 '18 at 12:58






1




1





It is not recommended to contain processing logic inside data classes, so I would recommend either to wrap Time data class instance with another object that knows how to parse, or making Time not data class and implement mentioned methods yourself.

– Andrey Ilyunin
Nov 27 '18 at 13:01





It is not recommended to contain processing logic inside data classes, so I would recommend either to wrap Time data class instance with another object that knows how to parse, or making Time not data class and implement mentioned methods yourself.

– Andrey Ilyunin
Nov 27 '18 at 13:01




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53499729%2fhow-to-add-setter-and-getter-to-member-variables-of-a-parcelable-class%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks