【问题标题】:String Concatenation Function in Dataclass not working as expected数据类中的字符串连接函数未按预期工作
【发布时间】:2020-07-14 11:44:14
【问题描述】:

我确定这是一个简单的原因,但我不明白为什么下面数据类中的函数 projectTitleText() 在我的 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
    }

}

我知道projectTitle 中有一个值,所以.isNotEmpty().plus() 函数没有按我的预期工作..

在我的适配器中,代码如下所示:

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

【问题讨论】:

  • 执行这段代码时projectTitle的值是多少?
  • 因此,当它从 Firestore 中提取数据时,它就是数据库中的任何内容(例如,对于 Title 的 Reference 'New Warehouse' 是 '22225')。所以 projectTitleText 应该显示 '22225 - New Warehouse'.. 为了清楚起见,我将添加在适配器中使用的代码..

标签: string kotlin concatenation isnullorempty


【解决方案1】:

Java(以及 Kotlin)的字符串是不可变的。根据documentationplus

返回通过将此字符串与给定其他对象的字符串表示形式连接获得的字符串。

意思是plus不会修改原来的String(它不能),它会产生一个新的。因此,您的代码必须更改为以下内容才能正常工作:

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

【讨论】:

  • 感谢 Egor 的完整回复 - 我知道这很简单,只是看不到问题。
猜你喜欢
  • 2023-04-03
  • 2021-03-31
  • 2017-07-21
  • 2018-02-01
  • 2011-05-17
  • 1970-01-01
  • 2023-02-03
  • 2012-10-31
  • 1970-01-01
相关资源
最近更新 更多