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?

Jelmer Brands
  • 192
  • 2
  • 9
  • 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? – George Mount Nov 02 '17 at 02: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 }` – Jelmer Brands Nov 02 '17 at 10:14

1 Answers1

17

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))} />
sanoop sandy
  • 534
  • 3
  • 12
  • 1
    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. – Jelmer Brands Nov 02 '17 at 10:21