Home > Enterprise >  Date Picker Dialog Code error in Android Kotlin
Date Picker Dialog Code error in Android Kotlin

Time:07-31

Below is the date picker dialog in one of the fragment and I am getting the error shown below

Type mismatch: inferred type is Int but LocalDate was expected
on line:
viewModel.onDateSelected(year, month, dayOfMonth)

 private val datePickerDialog by lazy {
        DatePickerDialog(requireActivity(), R.style.DatePicker).apply {
            setTitle(R.string.select_date)
            datePicker.maxDate = LocalDate.now().minusDays(0).toMillis()

            setOnDateSetListener { _, year, month, dayOfMonth ->
                viewModel.onDateSelected(year, month, dayOfMonth)
            }
        }
    }

CodePudding user response:

As unowsp pointed out, seems that you need a localDate. Try something like:

setOnDateSetListener { _, year, month, dayOfMonth ->
        val calendar = Calendar.getInstance()
        calendar.set(Calendar.YEAR, year)
        calendar.set(Calendar.MONTH, month)
        calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth)
        val localDate = LocalDateTime.ofInstant(calendar.toInstant(), calendar.timeZone.toZoneId())
            .toLocalDate()
        viewModel.onDateSelected(localDate)
    }
  • Related