我有一个几乎简单的想法:我想为带有数据绑定API和BindingAdapter的微调器生成一个适配器。这是我想要使用的XML:
bool flag = false;
for(i=0;i<imax;i++)
{ for(j=0;j<jmax(i);j++)
{ for(c=0;c<cmax(j);c++)
{ if(!check1(c))
{ if(check2()) {flag=true;break;}// goes to ni
else break; //goes to nj
}
}
if(flag){flag=false; break;}
nj:;
}
// procedure
ni:;
}
这里的地址是一个简单的类,它有一个名为<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}"/>
的字段,它是一个字符串,将包含一个ISO-3166-2字符串。为了保持简单,价值观将是&#34; DE&#34;或&#34;美国&#34;。
这是我简化的 country
:
arrays.xml
对于绑定我写了这个BindingAdapter:
<?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>
当我尝试编译代码时,我收到此错误:
错误:任务&#39;:app:compileDebugJavaWithJavac&#39;。执行失败。
java.lang.RuntimeException:发现数据绑定错误 **** /数据绑定错误****消息:标识符必须具有XML文件中的用户定义类型。国家缺少它 文件:路径/到/的/的旋above.xml
当前:95:31-95:39
**** \数据绑定错误****
我的xml的第95行是这一行:@BindingAdapter({"value", "data", "keys"})
public static void generateAdapter(Spinner spinner,
String value,
@ArrayRes int data,
@ArrayRes int keys) {
}
你看到我做错了吗?
顺便说一句,我不确定与数组资源相关的注释是否正确?我发现无法将其限制为字符串数组。
答案 0 :(得分:7)
您可以通过引用stringArray
代替array
来获取它。以下是我使用recyclelerView从资源中获取价值的方法,它运作良好,也可能对您有所帮助。
在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>
在layout.xml中
app:entries="@{@stringArray/fi}"
在您的情况下,它可以是app:data="@{@stringArray/countries}"
或app:keys="@{@stringArray/iso_3166_2}"
。
和绑定方法
@BindingAdapter({"entries"})
public static void entries(RecyclerView recyclerView, String[] array) {
//get all values in array[] variable.
}
请参阅this了解更多信息。
答案 1 :(得分:0)
如果要访问数据绑定布局文件中的其他资源类型,请参见源代码
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 + ")";
}