Search code examples
androidandroid-databinding

Data binding: Cannot bind ArrayAdpater to AppCompatAutoCompleteTextView


I have a AppCompatAutoCompleteTextView in my layout.xml file like so

<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}/>

The problem is when I try to build the project, I get the following error:

****/ 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 ****

But, if I remove the android:adapter line from the layout.xml file, and setup the binding from the activity directly, it works just fine. Like so,

AppCompatAutoCompleteTextView warehouseDropdown = findViewById(R.id.warehouseDropdown);
warehouseDropdown.setAdapter(viewModel.getWarehouseAdapter());

I have already tried clearing the IDE generated files as said here: Listener Binding; Cannot Find the Setter. Even this does not seem to solve my problem.


Solution

  • msg:Cannot find the setter for attribute 'android:adapter' with parameter type android.widget.ArrayAdapter on android.support.v7.widget.AppCompatAutoCompleteTextView

    As it says, I looked AutoCompleteTextViewBindingAdapter. and could not find any BindingAdapter related to android:adapter namespace. They have not yet defined it.

    You can create your Binding Adapter if you want.

    @BindingAdapter("android:adapter")
    public static void setAutoCompleteAdapter(AutoCompleteTextView textView, ArrayAdapter adapter) {
        textView.setAdapter(adapter);
    }