【发布时间】: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