skip to Main Content

I have a data class

@Parcelize
data class PublicationPageModel(
    @SerializedName("pageNumber")
    val pageNumber: Int,
    @SerializedName("content")
    val content: String
): Parcelable

I already added items to the array like this:

private var publicationPageModel: ArrayList<PublicationPageModel> = arrayListOf()

publicationPageModel.add(PublicationPageModel(finalPageNumber, fileContent))

Now, I want to add additional item to the fileContent already in the model above ↑

Say add a string "hello" to each of the content in the model

E.g if I have:

- PageModel("1", "content1")
- pageModel("2", "content2")

Now I want to have:

- PageModel("1", "hello content1")
- PageModel("2", "hello content2")

How to do this please?

2

Answers


  1. Since you want to add new value but only 1 String affected. Just append new value. Change the fileContent to var type.

    private var fileContent = "content"
    private var publicationPageModel: ArrayList<PublicationPageModel> = arrayListOf()
    
    fileContent = "hello $fileContent"
    publicationPageModel.add(PublicationPageModel(finalPageNumber, fileContent))
    
    Login or Signup to reply.
  2. You could try with MessageFormat.

    private var fileContent = "content1"
    private var publicationPageModel: ArrayList<PublicationPageModel> = arrayListOf()
    
    publicationPageModel.add(PublicationPageModel(finalPageNumber, MessageFormat.format("Hello {0}", fileContent)))
    

    You could event put that "Hello {0}" in String resources and when you need it retrieve it from there.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search