某些情况::我正在根据旅行的日期过滤旅行,这些旅行在firebase上,我有一个方法( getTripState )将当前日期与旅行日期,并根据情况返回“即将到来”或“旧旅行”。这些芯片充当启用单个选择的切换按钮。我根据芯片文本是否等于行程中的日期来过滤tripArray。即使String完全相同,比较也会返回false。
chipGroup.setOnCheckedChangeListener { chipGroup, i ->
val chip: Chip = chipGroup.findViewById(i)
rvHome.adapter = TripHistoryAdaptor(context, tripsArray.filter {
Toast.makeText(context, (it.date.getTripState() == chip.text).toString(), Toast.LENGTH_SHORT).show()
Toast.makeText(context, "${it.date.getTripState()} == ${chip.text} ", Toast.LENGTH_SHORT).show()
it.startAt.getTripState() === chip.text
} as ArrayList<Trip>)
(rvHome.adapter as TripHistoryAdaptor).notifyDataSetChanged()
}
答案 0 :(得分:0)
可能是由于case-sensitive comparison
引起的。看看我下面的例子
val first = "kotlin"
val second = "kotlin"
val firstCapitalized = "KOTLIN"
println(first == second) // output: True
print(first == firstCapitalized) // output: False [becuase uppercase and lowercase]
因此,您可以通过如下所示将字符串转换为lowercase
来比较字符串。
val first = "kotlin"
val second = "kotlin"
val firstCapitalized = "KOTLIN"
println(first == second) // output: True
print(first.toLowerCase() == firstCapitalized.toLowerCase()) // output: True
另一种方式,您可以将equals method
和pass true
用于second optional parameter
ignoreCase:
val first = "kotlin"
val second = "kotlin"
val firstCapitalized = "KOTLIN"
println(first.equals(second)) // output: True
print(first.equals(firstCapitalized, true)) // output: True