【问题标题】:How to highlight days from API in highlighable calendar in Kotlin?如何在 Kotlin 的高亮日历中突出显示 API 中的日期?
【发布时间】:2021-09-04 06:25:07
【问题描述】:

我有发布和获取日期的 API:

这是数据类:

data class PlannerGet(
val date: String,
val endTime: String,
val id: Int,
val location: String,
val note: String,
val startTime: String,
val title: String
)

我正在将此库用于日历: https://github.com/VarunBarad/Highlightable-Calendar-View

现在在片段中,我可以像这样突出显示某些日子:

HighlightableCalendarView.dayDecorators = listOf(
        DayDecorator(
            Calendar.getInstance().apply {
                set(Calendar.DAY_OF_MONTH, 4)
            },
            Color.parseColor("#ffffff"),
            Color.parseColor("#ff0000")
        ),
    )

但我想强调 API 的日子 我试着让它像这样:

HighlightableCalendarView.dayDecorators = listOf(
        DayDecorator(
            Calendar.getInstance().apply {
                set(PlannerGet.date)
            },
            Color.parseColor("#ffffff"),
            Color.parseColor("#ff0000")
        ),
    )

但我在“设置”时遇到问题,它显示“以下函数都不能使用提供的参数调用。”

我尝试添加“toInt()”,但还是同样的问题。

实现这一目标的正确方法是什么?

【问题讨论】:

标签: java android api date kotlin


【解决方案1】:

这是因为您传递的参数与所需的参数不匹配。

 Calendar.getInstance().apply {
            set(Calendar.DAY_OF_MONTH, 4)
        }

set 函数接受 int 字段,int 值,但您将参数作为字符串传递

PlannerGet.date

功能集

public void set(int field, int value) {
    throw new RuntimeException("Stub!");
}

如果您希望从 API 日期传递日期,请将字符串日期转换为 java Date 对象。

【讨论】:

    【解决方案2】:

    解决方案:

    if (response?.body().toString() == "[]") {
    
    
                }
    
                else if (response.isSuccessful) {
    
                    response.body()?.forEach {
    
                        getplanner.add(it)
                        Log.e("gggg gggg",getplanner.toString())
                        Log.e("gggg ddddd",getplanner[0].date)
    
                    }
    
                    val list = arrayListOf<DayDecorator>()
    
                    for (dsds in getplanner) {
    
    
    
                        list.add( DayDecorator(
                            Calendar.getInstance().apply {
    
                                //  getplanner[0].date
                                val input_date = dsds.date
                                val format1 = SimpleDateFormat("yyyy-MM-dd")
                                var dt1: Date? = null
    
                                dt1 = format1.parse(input_date)
                                val format2: DateFormat = SimpleDateFormat("dd")
                                val strMonth: String = format2.format(dt1)
                                val month = strMonth.toInt()
                                Log.e("dateinplanner", "" + month)
    
                                set(Calendar.DAY_OF_MONTH, month)
    
                            },
                            Color.parseColor("#ffffff"),
                            Color.parseColor("#1AB7B8")
    
                        ))
    
                    }
    
    
    
                    HighlightableCalendarView.dayDecorators = list
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-22
      • 2021-05-03
      • 2014-09-15
      • 1970-01-01
      • 2019-03-11
      • 1970-01-01
      相关资源
      最近更新 更多