How to add setter and getter to member variables of a Parcelable class
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
add a comment |
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
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
add a comment |
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
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
android kotlin
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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)
}
}
}
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 wrapTime
data class instance with another object that knows how to parse, or makingTime
not data class and implement mentioned methods yourself.
– Andrey Ilyunin
Nov 27 '18 at 13:01
|
show 3 more comments
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
}
}
}
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 wrapTime
data class instance with another object that knows how to parse, or makingTime
not data class and implement mentioned methods yourself.
– Andrey Ilyunin
Nov 27 '18 at 13:01
|
show 3 more comments
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)
}
}
}
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 wrapTime
data class instance with another object that knows how to parse, or makingTime
not data class and implement mentioned methods yourself.
– Andrey Ilyunin
Nov 27 '18 at 13:01
|
show 3 more comments
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)
}
}
}
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)
}
}
}
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 wrapTime
data class instance with another object that knows how to parse, or makingTime
not data class and implement mentioned methods yourself.
– Andrey Ilyunin
Nov 27 '18 at 13:01
|
show 3 more comments
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 wrapTime
data class instance with another object that knows how to parse, or makingTime
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
|
show 3 more comments
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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