我的 layout.xml 文件中有一个 AppCompatAutoCompleteTextView,就像这样
<android.support.v7.widget.AppCompatAutoCompleteTextView
android:id="@+id/warehouseDropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/warehouse"
android:adapter="@{viewModel.warehouseAdapter}/>
问题是当我尝试构建项目时,出现以下错误:
****/ data binding error ****msg:Cannot find the setter for attribute 'android:adapter' with parameter type android.widget.ArrayAdapter on android.support.v7.widget.AppCompatAutoCompleteTextView. file:.../app/src/main/res/layout/activity_scan.xml loc:52:43 - 52:68 ****\ data binding error ****
但是,如果我从 layout.xml 文件中删除 android:adapter
行,并直接从 Activity 设置绑定(bind),它就可以正常工作。像这样,
AppCompatAutoCompleteTextView warehouseDropdown = findViewById(R.id.warehouseDropdown);
warehouseDropdown.setAdapter(viewModel.getWarehouseAdapter());
我已经尝试按照此处所述清除 IDE 生成的文件: Listener Binding; Cannot Find the Setter .即使这样似乎也无法解决我的问题。
最佳答案
msg:Cannot find the setter for attribute 'android:adapter' with parameter type android.widget.ArrayAdapter on android.support.v7.widget.AppCompatAutoCompleteTextView
如其所说,我看了AutoCompleteTextViewBindingAdapter
.并且找不到与 android:adapter
命名空间相关的任何 BindingAdapter。他们还没有定义它。
您可以创建自己的 Binding Adapter如果你愿意的话。
@BindingAdapter("android:adapter")
public static void setAutoCompleteAdapter(AutoCompleteTextView textView, ArrayAdapter adapter) {
textView.setAdapter(adapter);
}
关于android - 数据绑定(bind) : Cannot bind ArrayAdpater to AppCompatAutoCompleteTextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52625778/