5

I want to achieve two-way binding on a array with Data Binding in Android.

This is a simplified version of the code I have:

<data>
    <variable
        name="values"
        type="Integer[]" />
</data>

<EditText
    ...
    android:text="@={Converter.toString(values[0])} />

But when I try to build this code I get a message as follows:

cannot find method setTo(java.lang.Integer[], int, java.lang.Integer) in class android.databinding.ViewDataBinding

How can I achieve two-way binding with an array, if at all possible?

2
  • Weird. That method exists in ViewDataBinding.java: void setTo(T[] arr, int index, T value) -- you can check yourself. Are you somehow specifying a different library dependency than expected from the gradle plugin? Commented Nov 2, 2017 at 2:06
  • I checked and indeed found the method you mentioned. Where can I see the library dependancy, and what is it supposed to be. I only used these lines of code in my app's build.gradle dataBinding { enabled = true } Commented Nov 2, 2017 at 10:14

1 Answer 1

18

How about trying the following way by using ArrayList.

<data>
    <import type="java.util.ArrayList"/>
    <variable
        name="values"
        type="ArrayList&lt;Integer&gt;"/>
</data>

<EditText
    ...
    android:text="@={Converter.toString(values.get(0))} />
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @sanoop, this worked for me. I was stuck in my head trying to figure out why Integer[] didn't work because I have seen @George Mount using it in an accepted answer for another post.

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.