ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – 使用DataBinding访问整数数组

2019-06-23 05:21:11  阅读:566  来源: 互联网

标签:android data-binding android-databinding


我正在尝试使用DataBinding for Integer数据创建RecyclerView,我已经尝试使用String数据通过在string.xml中声明字符串数组,here是我已经参考的答案.

现在我试图用整数数组实现它,但无法从xml访问它.

这是我的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}">

和我的自定义绑定适配器

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

但它显示错误行1:0令牌识别错误:’@ 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

解决方法:

document本身找到解决方案,我犯了愚蠢的错误.它应该是intArray而不是integerArray,而在BindingAdapter中它应该是int []

app:entries="@{@intArray/hours}"

和适配器应该是

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

标签:android,data-binding,android-databinding
来源: https://codeday.me/bug/20190623/1267929.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有