13

To use multiple arguments for a data binding adapter the Java syntax is

@BindingAdapter(value={"arg1", "arg2"}, requireAll = false)

However this does not compile in Kotlin:

Error:(13, 37) Unexpected tokens (use ';' to separate expressions on the same line)

what's the correct syntax for multiple arguments in Kotlin?

4 Answers 4

17

Should be:

@BindingAdapter(value=*arrayOf("arg1", "arg2"), requireAll = false)

Please refer to the official annotations docs for Java Annotations in Kotlin

Quoting:

For other arguments that have an array type, you need to use arrayOf explicitly:

// Java
public @interface AnnWithArrayMethod {
    String[] names();
}


// Kotlin
@AnnWithArrayMethod(names = arrayOf("abc", "foo", "bar")) class C

EDIT: Credit to @Francesc

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. The syntax is actually slightly different, @BindingAdapter(value= *arrayOf("arg1", "arg2"), requireAll = false)
Please note that requireAll argument must be set explicitly, that was the problem in my case
8

Or you can just simply do this

@BindingAdapter("arg1", "agr2", "agr3", "agr4", requireAll = false)

as pointed in Android Official Docs

Comments

5

From Kotlin 1.2 you can do

@BindingAdapter(value = ["arg1", "arg2"], requireAll = false)

1 Comment

can you specifiy a source?
1

You can also do it by this way:

@BindingAdapter(value= ["deckBackgroundAsFirstParameter", "typeAsSecondParameter"], requireAll = false) 
fun loadBackgroundMethodNameForExample(imageViewForExample: ImageView, deckBackgroundAsFirstParameter: Int?, typeAsSecondParameter: Int?) {
   ...
}

where value is an array of the parameters that you want to use.

1 Comment

While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.