首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用来自资源的字符串数组的BindingAdapter。

使用来自资源的字符串数组的BindingAdapter。
EN

Stack Overflow用户
提问于 2016-04-27 20:10:39
回答 2查看 6.9K关注 0票数 13

我有一个几乎简单的想法:我想用数据绑定API和BindingAdapter为旋转器生成一个Adapter。下面是我想使用的XML:

代码语言:javascript
复制
<Spinner
    android:id="@+id/country"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:value="@{address.country}"
    app:data="@{@array/countries}"
    app:keys="@{@array/iso_3166_2}"/>

Address这里是一个简单的类,它有一个名为country的字段,它是一个字符串,将包含一个ISO-3166-2字符串。为了保持简单,值将是"DE“或"US”。

这是我简化的arrays.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="iso_3166_2">
        <item>DE</item>
        <item>US</item>
    </string-array>

    <string-array name="countries">
        <item>@string/country_DE</item>
        <item>@string/country_US</item>
    </string-array>
</resources>

对于绑定,我编写了这个BindingAdapter:

代码语言:javascript
复制
@BindingAdapter({"value", "data", "keys"})
public static void generateAdapter(Spinner spinner,
                                   String value,
                                   @ArrayRes int data,
                                   @ArrayRes int keys) {

}

当我试图编译代码时,我会得到以下错误:

错误:任务执行失败“:app:compileDebugJavaWithJavac”。 java.lang.RuntimeException:发现数据绑定错误。 */数据绑定错误*msg:标识符必须有来自XML文件的用户定义类型。各国都错过了 文件:path/to/spner-above.xml loc:95:31 - 95:39 *数据绑定错误*

我的xml的第95行是以下一行:app:value="@{address.country}"

你看到我做错什么了吗?

顺便说一句,我不确定与数组资源相关的注释是正确的吗?我找不到方法将其限制为字符串数组。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-04-28 04:30:08

您可以通过引用stringArray而不是array来获得它。下面是我对recyclerView所做的从资源中获取价值的方法,它运行得很好,可能也会对您有所帮助。

在string.xml中

代码语言:javascript
复制
<string-array name="myItems">
    <item>Item 1</item>
    <item>Item 2</item>
    <item>Item 3</item>
    <item>Item 4</item>
    <item>Item 5</item>
    <item>Item 6</item>
</string-array>

在layout.xml中

代码语言:javascript
复制
app:entries="@{@stringArray/fi}"

在您的例子中,可以是app:data="@{@stringArray/countries}"app:keys="@{@stringArray/iso_3166_2}"

结合法

代码语言:javascript
复制
@BindingAdapter({"entries"})
public static void entries(RecyclerView recyclerView, String[] array) {
    //get all values in array[] variable.
}

有关更多信息,请参考this

票数 14
EN

Stack Overflow用户

发布于 2020-11-19 11:40:26

如果要访问数据库布局文件中的其他资源类型,请参见源

代码语言:javascript
复制
    public String toJava() {
        final String context = "getRoot().getContext()";
        final String viewName = requiresView() ? LayoutBinderWriterKt.getFieldName(mTarget) :
                "getRoot()";
        final String resources = viewName + ".getResources()";
        final String resourceName = mPackage + "R." + getResourceObject() + "." + mResourceId;
        if ("anim".equals(mResourceType)) return "android.view.animation.AnimationUtils.loadAnimation(" + context + ", " + resourceName + ")";
        if ("animator".equals(mResourceType)) return "android.animation.AnimatorInflater.loadAnimator(" + context + ", " + resourceName + ")";
        if ("bool".equals(mResourceType)) return resources + ".getBoolean(" + resourceName + ")";
        if ("color".equals(mResourceType)) return "android.databinding.DynamicUtil.getColorFromResource(" + viewName + ", " + resourceName + ")";
        if ("colorStateList".equals(mResourceType)) return "android.databinding.DynamicUtil.getColorStateListFromResource(" + viewName + ", " + resourceName + ")";
        if ("dimen".equals(mResourceType)) return resources + ".getDimension(" + resourceName + ")";
        if ("dimenOffset".equals(mResourceType)) return resources + ".getDimensionPixelOffset(" + resourceName + ")";
        if ("dimenSize".equals(mResourceType)) return resources + ".getDimensionPixelSize(" + resourceName + ")";
        if ("drawable".equals(mResourceType)) return "android.databinding.DynamicUtil.getDrawableFromResource(" + viewName + ", " + resourceName + ")";
        if ("fraction".equals(mResourceType)) {
            String base = getChildCode(0, "1");
            String pbase = getChildCode(1, "1");
            return resources + ".getFraction(" + resourceName + ", " + base + ", " + pbase +
                    ")";
        }
        if ("id".equals(mResourceType)) return resourceName;
        if ("intArray".equals(mResourceType)) return resources + ".getIntArray(" + resourceName + ")";
        if ("integer".equals(mResourceType)) return resources + ".getInteger(" + resourceName + ")";
        if ("interpolator".equals(mResourceType))  return "android.view.animation.AnimationUtils.loadInterpolator(" + context + ", " + resourceName + ")";
        if ("layout".equals(mResourceType)) return resourceName;
        if ("plurals".equals(mResourceType)) {
            if (getChildren().isEmpty()) {
                return resourceName;
            } else {
                return makeParameterCall(resources, resourceName, "getQuantityString");
            }
        }
        if ("stateListAnimator".equals(mResourceType)) return "android.animation.AnimatorInflater.loadStateListAnimator(" + context + ", " + resourceName + ")";
        if ("string".equals(mResourceType)) return makeParameterCall(resources, resourceName, "getString");
        if ("stringArray".equals(mResourceType)) return resources + ".getStringArray(" + resourceName + ")";
        if ("transition".equals(mResourceType)) return "android.transition.TransitionInflater.from(" + context + ").inflateTransition(" + resourceName + ")";
        if ("typedArray".equals(mResourceType)) return resources + ".obtainTypedArray(" + resourceName + ")";
        final String property = Character.toUpperCase(mResourceType.charAt(0)) +
                mResourceType.substring(1);
        return resources + ".get" + property + "(" + resourceName + ")";

    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36899942

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档