Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – kotlin: Mutable property type in a data class

Posted on November 11, 2022 By

Solution 1 :

Not quite sure if this is the right answer to your question, but I think that a sealed class may help you.
Basically you could define a MutableUser (User or string) and each subclass of MutableUser should implement Parcelable.

sealed class MutableUser : Parcelable {
    @Parcelize
    class UserAttribute(val user: User) : MutableUser()

    @Parcelize
    class SimpleAttribute(val userId: String) : MutableUser()
}

Then, you could use this MutableUser in your car data model like below:

@Parcelize
data class Car(
   val createdAt: String? = "",
   val updatedAt: String? = "",
   val id: String? = "",
   val name: String? = "",
   val user: MutableUser
) : Parcelable

Problem :

I have the following data class that implements Parcelable:

data class Car(
    val createdAt: String? = "",
    val updatedAt: String? = "",
    val id: String? = "",
    val name: String? = "",
    val user: User? = User()
)

In this data class, sometimes the user attributes can be the id of the user (String) and sometimes it could be the User object itself.

Is there any way to implement Parcelable and indicates that the value can be either a String or an Object ? (Another data model)

Comments

Comment posted by Blackbelt

User should be parcelable as well. When you unparcel Car you might have both or just one of the two information.

READ  [FIXED] android - Change image alpha from listview position and save
Powered by Inline Related Posts
Android Tags:android, data-class, kotlin, parcelable

Post navigation

Previous Post: [FIXED] Xamarin.forms – Trouble behaviour between ListView & keyboard
Next Post: [FIXED] android – two elements in linear layout are packed to center instead of to the sides

Related Posts

[FIXED] android – ViewPager2 error androidx….RecycleView.Adapter) in ViewPager2 can not be applied Android
[FIXED] java – How to return from Glide onResourceReady Android
[FIXED] android jetpack compose – How does this function keep track of clicks? Android
[FIXED] android – Trying to make my application device owner in simulator Android
[FIXED] c# – Missing dll System.Net.Security.Native in Xamarin Android app during SQL connection Android
[FIXED] android – Cannot convert between a TensorFlowLite buffer with 307200 bytes and a Java Buffer with 270000 bytes Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What is a good substitute for condensed milk?
  • ¿Como el cuerpo te avisa cuando te va a dar un infarto?
  • What is the luxury brand of Jaguar?
  • Who is Big poppa baseball player?
  • What material are foam runners?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme