1

I want to initialize the value of image when data is fetched from server I tried this but value isn't updating.

@Entity(tableName = "categories")
data class Category(
    @PrimaryKey(autoGenerate = false)
    val name: String,
    var image: String,
){
   init{
     image = "someUrl" + image
   } 
}

2 Answers 2

1

I guess you are trying to have image save the full path of some URL.

An idea to achieve this is to have a backing field into Category class like the following


@Entity(tableName = "categories")
data class Category(
    @PrimaryKey(autoGenerate = false)
    val name: String,
    var image: String,
){
   val imageUrl: String
       get() = "some Url" + image
}

Then when you want to load the image you could have

val item:Category
imgViwew.load(item.imageUrl)
Sign up to request clarification or add additional context in comments.

Comments

0

You can solve this with the help of JPA lifecycle events, like this:

@Entity(tableName = "categories")
data class Category(
    @PrimaryKey(autoGenerate = false)
    val name: String,
    var image: String,
){

   @PostLoad // <---
   fun postLoad() {
     image = "someUrl" + image
   } 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.