Home > Mobile >  User kotlin lib in java call fun
User kotlin lib in java call fun

Time:01-25

I use a librery that is writed by kotlin in android studio project

    class Builder<S> private constructor(val dateSelector: DateSelector<S>) {
     var calendarConstraints: CalendarConstraints? =null
    /** Sets the first, last, and starting [Month].  */
    fun setCalendarConstraints(bounds: CalendarConstraints?): Builder<S> {
        calendarConstraints = bounds
        return this
    }
    /** Creates a [MaterialDatePicker] with the provided options.  */
     companion object {            
        /**
         * Used to create a Builder that allows for choosing a single date in the `MaterialDatePicker`.
         */
        fun datePicker(): Builder<Long?> {
            return Builder(SingleDateSelector())
        }
    }
}

when i want to use setCalendarConstraints in java class activity

MaterialDatePicker.Builder.Companion.datePicker().setTitleText(R.string.selectdate).setCalendarConstraints(constraints).build();

i have this error for when call setCalendarConstraints

Ambiguous method call. Both setCalendarConstraints (CalendarConstraints) in Builder and setCalendarConstraints (CalendarConstraints) in Builder match

which i can split default setter and fun in java?

CodePudding user response:

If you have access to the library, I guess only the following alternative options can help, modifying the source code of the library:

  1. Make calendarConstraints property private.

  2. Rename calendarConstraints property to something else.

  3. Rename setCalendarConstraints method to something else.

  4. Add some additional parameter to setCalendarConstraints method.

  5. Add @JvmField annotation to calendarConstraints property:

    @JvmField
    var calendarConstraints: CalendarConstraints? = null
    

Or you can use Kotlin instead of Java in the project where you are working with this library.

CodePudding user response:

Try this if it helps

Builder<Long> materialDatePickerBuilder = MaterialDatePicker.Builder.Companion.datePicker();
materialDatePickerBuilder.setTitleText(R.string.selectdate);
materialDatePickerBuilder.setCalendarConstraints(constraints);

// pass materialDatePickerBuilder.build() whenever you need it

  •  Tags:  
  • Related