Search code examples
stringkotlinconcatenationisnullorempty

String Concatenation Function in Dataclass not working as expected


I'm sure a simple reason for this, but I can't see why the function projectTitleText() in below dataclass isn't concatenating as expected in my recyclerView:

data class ProjectObject (
    var projectReference: String = "NO REFERENCE",
    var projectTitle: String = "", // e.g. "C & F Beam Reconfiguration"
    var projectDescription: String = "", // e.g. "Order for beam reconfiguration project"
    var projectStatus: String = "Enquiry",
    var projectNotes: String = "",
    var projectTask: Boolean = false,

    var createdBy: String = "",
    var users: HashMap<String, Boolean> = hashMapOf(),
    var sites: HashMap<String, Boolean> = hashMapOf(),
    var contacts: HashMap<String, Boolean> = hashMapOf(),

    @ServerTimestamp
    var dateCreatedTimestamp: Date? = null,
    @ServerTimestamp
    var dateEditedTimestamp: Date? = null,

    @Exclude @set:Exclude @get:Exclude
    var projectID: String = ""

) : Serializable {

    override fun toString(): String {
        return projectReference
    }

    fun projectTitleText(): String {
        var projectTitleText = projectReference
        if (projectTitle.isNotEmpty()) projectTitleText.plus(" - $projectTitle")
        return projectTitleText
    }

}

I know that there is a value in projectTitle so either .isNotEmpty() or .plus() functions are not working as I expected..

In my adapter, the code looks like this:

    class ProjectViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bind(
            project: ProjectObject,
            clickListener: (ProjectObject) -> Unit,
            longClickListener: (ProjectObject) -> Boolean
        ) {
            // Set text fields
            itemView.projectsItemTitleText.text = project.projectTitle
            itemView.projectsItemDetailsText.text = project.projectDescription
            itemView.projectsItemContactsText.text = project.projectContactsText() // Not working!!
            itemView.projectsStatusButton.text = project.projectStatus

            // Set Task Icon visibility
            if (project.projectTask) itemView.projectsItemTaskImageView.visibility = View.VISIBLE
            else itemView.projectsItemTaskImageView.visibility = View.INVISIBLE

            //Set Status background colour
            when (project.projectStatus) {
                "Enquiry" -> itemView.projectsStatusButton.setBackgroundColor(ContextCompat.getColor(itemView.context, R.color.Enquiry))
                "Quote" -> itemView.projectsStatusButton.setBackgroundColor(ContextCompat.getColor(itemView.context, R.color.Quote))
                "Order" -> itemView.projectsStatusButton.setBackgroundColor(ContextCompat.getColor(itemView.context, R.color.Order))
                "Dead" -> itemView.projectsStatusButton.setBackgroundColor(ContextCompat.getColor(itemView.context, R.color.Dead))
                "Lost" -> itemView.projectsStatusButton.setBackgroundColor(ContextCompat.getColor(itemView.context, R.color.Lost))
            }

            // Set Listeners
            itemView.setOnClickListener { clickListener(project) }
            itemView.setOnLongClickListener { longClickListener(project) }
        }
    }


Solution

  • Java's (and hence Kotlin's) Strings are immutable. According to the documentation, plus

    Returns a string obtained by concatenating this string with the string representation of the given other object.

    meaning that plus will not modify the original String (it can't), it will produce a new one. Your code, therefore, has to be changed to the following to work properly:

    fun projectTitleText(): String {
      var projectTitleText = projectReference
      if (projectTitle.isNotEmpty()) projectTitleText = projectTitleText.plus(" - $projectTitle")
      return projectTitleText
    }