我正在尝试使用新的 Android 数据绑定(bind)库,并在尝试使用所选值填充微调器时遇到以下错误。
错误消息(在 Android Studio 中编译期间):
Error:Execution failed for task ':app:compileDebugJavaWithJavac'. java.lang.RuntimeException: Found data binding errors. ****/ data binding error ****msg:Cannot find the setter for attribute 'app:selection' with parameter type java.lang.String. file:/Users/ove/Code/AndroidStudio/Samples/Receipts/app/src/main/res/layout/dialogfragment_inputamount_db.xml loc:40:29 - 40:44 ****\ data binding error ****
我的布局文件如下所示(不完整):
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="receipt"
type="com.example.model.Receipt" />
</data>
</LinearLayout>
<Spinner
android:layout_width="wrap_content"
android:id="@+id/currency"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"
android:entries="@array/currency_array"
app:selection="@{receipt.currency}" />
</LinearLayout>
</layout>
有人设法让数据绑定(bind)与微调器一起工作吗?
结束
最佳答案
创建类 BindingUtils 并粘贴 setSelection 方法
public class BindingUtils
{
@BindingAdapter({"bind:selection"})
public static void setSelection(Spinner spinner, int position)
{
spinner.setSelection(position);
}
}
内部微调器
app:selection="@{receipt.currencyIdx}"
这就是你所要做的。
关于java - 如何将 DataBindingUtil 与 Android 微调器一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32996263/