Home > Enterprise >  Add additional item to an item in model class array list
Add additional item to an item in model class array list

Time:04-13

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?

CodePudding user response:

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))

CodePudding user response:

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.

  • Related