Reputation: 35559
I am trying to create RecyclerView
with use of DataBinding for Integer data, i have already tried with String data by declaring string-array
in string.xml
, here is answer from where i have taken reference.
Now i am trying to implement it with integer-array
but not able to access it from xml.
Here is my integer.xml
<integer-array name="hours">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
<item>9</item>
<item>10</item>
<item>11</item>
<item>12</item>
</integer-array>
here is my xml file
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:entries="@{@integerArray/hours}">
and my custom binding adapter
@BindingAdapter("entries")
public static void entries(RecyclerView recyclerView, Integer[] array) {
recyclerView.setAdapter(new SimpleArrayAdapter(array));
}
But it is showing me error line 1:0 token recognition error at: '@integerArray/'
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> java.lang.RuntimeException: Found data binding errors.
****/ data binding error ****msg:Identifiers must have user defined types from the XML file. hours is missing
Upvotes: 5
Views: 2265
Reputation: 8857
Thanks for the answer. Here I want to share the similar solution for binding image from integer-array
Drawable
resource.
My integer-array
resource of Drawable
:
<resources>
<integer-array name="platform_images">
<item>@drawable/android</item>
<item>@drawable/iphone</item>
<item>@drawable/windows</item>
</integer-array>
</resources>
My BindingAdapter
function:
object DataBindingUtil {
@BindingAdapter("imageIndex", "images")
@JvmStatic
fun setImageResource(imageView: ImageView, imageIndex: Int, images: TypedArray) {
imageView.setImageResource(images.getResourceId(imageIndex, 0))
}
}
My ImageView
:
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/android"
app:imageIndex="@{2}"
app:images="@{@typedArray/platform_images}" />
Upvotes: 0
Reputation: 35559
Found solution from document itself, i was doing silly mistake. It should be intArray
instead of integerArray
, and in BindingAdapter
it should be int[]
app:entries="@{@intArray/hours}"
and adapter should be
@BindingAdapter("entries")
public static void entries(RecyclerView recyclerView, int[] array) {
recyclerView.setAdapter(new SimpleArrayAdapter(array));
}
Upvotes: 6