使用DataBinding访问整数数组 [英] access integer array using DataBinding

查看:64
本文介绍了使用DataBinding访问整数数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用DataBinding的Integer数据创建RecyclerView,我已经通过在string.xml中声明string-array来尝试使用String数据,

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.

现在,我正在尝试使用integer-array实施它,但无法从xml访问它.

Now i am trying to implement it with integer-array but not able to access it from xml.

这是我的integer.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>

这是我的xml文件

<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));
}

但这显示了错误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

推荐答案

文档本身,我犯了愚蠢的错误.它应该是intArray而不是integerArray,在BindingAdapter中应该是int[]

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}"

并且适配器应该是

@BindingAdapter("entries")
public static void entries(RecyclerView recyclerView, int[] array) {
    recyclerView.setAdapter(new SimpleArrayAdapter(array));
}

这篇关于使用DataBinding访问整数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆