因此,我有3个文本视图来显示未来3天的预测。我使用视图绑定来绑定视图,但我觉得有一种更简单的方法来编写它,但我想不出解决方案。如何迭代一周中的几天并相应地设置文本视图?
'''
// getting day of the week
private val dayOfWeek: DayOfWeek = LocalDate.now().dayOfWeek
// getting day of week as int value
private val dayNum: Int = dayOfWeek.value
'''
''' // 1 = monday etc
private fun forecastDays() {
when (dayNum){
// forecast of next three days
1 -> {
binding.tvDay1.text = "Tuesday"
binding.tvDay2.text = "Wednesday"
binding.tvDay3.text = "Thursday"
}
2 -> {
binding.tvDay1.text = "Wednesday"
binding.tvDay2.text = "Thursday"
binding.tvDay3.text = "Friday"
}
3 -> {
binding.tvDay1.text = "Thursday"
binding.tvDay2.text = "Friday"
binding.tvDay3.text = "Saturday"
}
4 -> {
binding.tvDay1.text = "Friday"
binding.tvDay2.text = "Saturday"
binding.tvDay3.text = "Sunday"
}
.. etc
'''
2条答案
按热度按时间piok6c0g1#
你可以用它来做
DateFormatter
我想是吧。下面是另一种使用map的方法。你可以daysMap
作为常数。iyfjxgzm2#
dayofweek是一个枚举,这意味着您可以使用序号作为索引以数字形式获取dayofweek。