问题
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
回答1:
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));
}
来源:https://stackoverflow.com/questions/38989287/access-integer-array-using-databinding