数据类中的字符串连接函数未按预期工作
String Concatenation Function in Dataclass not working as expected
我确定这是一个简单的原因,但我不明白为什么下面数据类中的函数 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) }
}
}
Java 的(以及 Kotlin 的)字符串是不可变的。根据documentation、plus
Returns a string obtained by concatenating this string with the string representation of the given other object.
意思是plus
不会修改原来的String(不能),它会产生一个新的。因此,您的代码必须更改为以下内容才能正常工作:
fun projectTitleText(): String {
var projectTitleText = projectReference
if (projectTitle.isNotEmpty()) projectTitleText = projectTitleText.plus(" - $projectTitle")
return projectTitleText
}
我确定这是一个简单的原因,但我不明白为什么下面数据类中的函数 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) }
}
}
Java 的(以及 Kotlin 的)字符串是不可变的。根据documentation、plus
Returns a string obtained by concatenating this string with the string representation of the given other object.
意思是plus
不会修改原来的String(不能),它会产生一个新的。因此,您的代码必须更改为以下内容才能正常工作:
fun projectTitleText(): String {
var projectTitleText = projectReference
if (projectTitle.isNotEmpty()) projectTitleText = projectTitleText.plus(" - $projectTitle")
return projectTitleText
}