Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 345 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Add optional parameter to Android Kotlin class

#1
I've created an extension of `DialogFragment()`:


class AlertDialogFragment(context: Context,
val positiveButtonText: String,
val positiveButtonListener: DialogInterface.OnClickListener,
val negativeButtonText: String,
val negativeButtonListener: DialogInterface.OnClickListener,
neutralButtonText: String,
neutralButtonListener: DialogInterface.OnClickListener
) : DialogFragment() {

however I want the last 2 parameters to be optional.

How can I achieve this?

I can't set `neutralButtonListener: DialogInterface.OnClickListener = null` because `DialogInterface.OnClickListener` is a non null type.

Reply

#2
In general you would provide no-op or null-value implementations like

neutralButtonText: String = ""
neutralButtonListener: OnClickListener = OnClickListener {}

But in your use-case you **must not** use constructor parameters at all!

Fragments can be recreated from the system and require a default constructor.

You need to [communicate with the `activity` or `parentFragment`][1] by letting these implement the interface you require.


[1]:

[To see links please register here]

Reply

#3
We can add the optional paramater like below. z is optional here



fun main() {
add(5,6,7)
}

fun add(x:Int,y:Int,z:Int?=0){
if(z!=null && z>0)
println(x+y+z)
else
println(x+y)
}
Reply

#4
If you want to call kotlin function from Java file which has default arguments

Then use this annotation for your kotlin function

@JvmOverloads
Reply

#5
Default parameters to the rescue.

class AlertDialogFragment(
context: Context,
val positiveButtonText: String,
val positiveButtonListener: DialogInterface.OnClickListener,
val negativeButtonText: String,
val negativeButtonListener: DialogInterface.OnClickListener,
neutralButtonText: String = "",
neutralButtonListener: DialogInterface.OnClickListener = OnClickListener {}
) : DialogFragment()

Basically, Kotlin will generate multiple methods, for each combination of parameters possible.

If could also be better to include `@JvmOverload` annotation on the constructor to allow the same thing in Java.
Reply

#6
Quite simple...

For class use
```
class MyClass(param1: Int, param2: Boolean) {
constructor(param1: Int) : this(param1, true)
constructor(param2: Boolean) : this(10, param2)
}
```

and for functions

@JvmOverloads
fun yourMethod(param1: Int, param2: Boolean = true) {
// ... define your method
}

Now you can call this method any of the below

`yourMethod(1)`

`yourMethod(1, true)`

`yourMethod(param1 = 1, param2 = false)` etc.,
Reply

#7
Check this out

class AlertDialogFragment(
context: Context,
val positiveButtonText: String,
val positiveButtonListener: DialogInterface.OnClickListener,
val negativeButtonText: String,
val negativeButtonListener: DialogInterface.OnClickListener,
neutralButtonText: String = "",
neutralButtonListener: DialogInterface.OnClickListener ?= null
) : DialogFragment() { }
Reply

#8
You may try this:

class AlertDialogFragment(
context: Context,
val positiveButtonText: String,
val positiveButtonListener: DialogInterface.OnClickListener,
val negativeButtonText: String,
val negativeButtonListener: DialogInterface.OnClickListener,
neutralButtonText: String = "",
neutralButtonListener: DialogInterface.OnClickListener = OnClickListener {}
) : DialogFragment() { }
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through