2

When using anko selector as provided in the examples it is not working

val countries = listOf("Russia", "USA", "Japan", "Australia")
selector("Where are you from?", countries) { i ->
    toast("So you're living in ${countries[i]}, right?")
}

here when defining the lambda function it says expected two types of arguments. (Dialoginterface and int). I am stuck with this. Alos the default kotlin alertdialog is saying the same thing. Can anywone solve this issue or tell me how to build an alert dialog with selection in kotlin?

Sidharth Anil
  • 768
  • 1
  • 6
  • 14

1 Answers1

2

Seems like it's a mistake in the example.

The selector function source says it expects (DialogInterface, Int) -> Unit, a function with two parameters, so you can fix your code by adding the missing parameter as follows:

selector("Where are you from?", countries) { dialogInterface, i -> /* ... */ }

Or, if you don't use the DialogInterface, just ignore it with an underscore:

selector("Where are you from?", countries) { _, i -> /* ... */ }
hotkey
  • 140,743
  • 39
  • 371
  • 326
  • Thanks a lot. One more thing what is the use of this dialog interface? – Sidharth Anil Jun 07 '17 at 14:59
  • I believe it's for a case when you want to call its [`cancel()` or `dismiss()`](https://stackoverflow.com/questions/12139335/what-is-difference-between-dialoginterface-dismiss-and-dialoginterface-can) in your event handler. I found nothing else in [its docs](https://developer.android.com/reference/android/content/DialogInterface.html) that would look useful. – hotkey Jun 07 '17 at 15:34
  • I'm getting an unresolved reference for selector. I am including dependencies for ank-commons as instructed by the docs. Anyone else getting that? – airowe Sep 22 '17 at 13:50
  • Looks like I need to include the meta-dependency too, in case anyone stumbles on this: compile"org.jetbrains.anko:anko:$anko_version" – airowe Sep 22 '17 at 14:00