Android 使用资源中带有字符串数组的BindingAdapter
我有一个几乎很简单的想法:我想用数据绑定API和BindingAdapter为微调器生成一个适配器。以下是我要使用的XML:Android 使用资源中带有字符串数组的BindingAdapter,android,arrays,android-resources,android-databinding,Android,Arrays,Android Resources,Android Databinding,我有一个几乎很简单的想法:我想用数据绑定API和BindingAdapter为微调器生成一个适配器。以下是我要使用的XML: <Spinner android:id="@+id/country" android:layout_width="wrap_content" android:layout_height="wrap_content" app:value="@{address.country}" app:data="@{@array/countr
<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}"/>
对于绑定,我编写了以下BindingAdapter:
@BindingAdapter({"value", "data", "keys"})
public static void generateAdapter(Spinner spinner,
String value,
@ArrayRes int data,
@ArrayRes int keys) {
}
当我试图编译代码时,出现以下错误:
错误:任务“:app:compiledBugJavaWithJavaC”的执行失败。java.lang.RuntimeException:发现数据绑定错误。
****/数据绑定错误****消息:标识符必须具有XML文件中的用户定义类型。国家缺少它
文件:path/to/the/spinner over.xml
loc:95:31-95:39
****\数据绑定错误**** 我的xml的第95行是这一行:
app:value=“@{address.country}”
你知道我做错了什么吗?
顺便问一下,我不确定与数组资源相关的注释是否正确?我没有办法将它限制为字符串数组。您可以通过引用
stringArray
而不是array
来获得它。以下是我使用recyclerView所做的工作,以从资源中获取价值,它工作得非常完美,可能也会对您有所帮助
在string.xml中
<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>
在您的情况下,它可以是app:data=“@{@stringArray/countries}”
或app:keys=“@{@stringArray/iso3166_2}”
在绑定方法中
@BindingAdapter({"entries"})
public static void entries(RecyclerView recyclerView, String[] array) {
//get all values in array[] variable.
}
有关详细信息,请参阅。如果要访问数据绑定布局文件中的其他资源类型,请参阅源代码
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 + ")";
}
@BindingAdapter({"entries"})
public static void entries(RecyclerView recyclerView, String[] array) {
//get all values in array[] variable.
}
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 + ")";
}