芯片setoncheckedchangelistener字符串比较返回false

时间:2019-10-29 04:16:59

标签: android kotlin android-chips

enter image description here

某些情况::我正在根据旅行的日期过滤旅行,这些旅行在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()
    }

1 个答案:

答案 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 methodpass 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