Search code examples
androidandroid-databindingandroid-binding-adapter

Android BindingAdapter error.NonExistentClass


Here it is my BindingAdapter

@InverseBindingMethods({
        @InverseBindingMethod(type = RecyclerView.class, attribute = "lastAdapter", method = "getLastAdapter"),
})
public class RecyclerViewBindingAdapter {
    @BindingAdapter(value = {"lastAdapter"}, requireAll = false)
    public static void setLastAdapter(RecyclerView view, LastAdapter adapter) {
        view.setAdapter(adapter);
    }

    @InverseBindingAdapter(attribute = "lastAdapter")
    public static LastAdapter getLastAdapter(RecyclerView view) {
        return (LastAdapter) view.getAdapter();
    }
}

I have added RecyclerView dependency on my gradle:

compile "com.android.support:recyclerview-v7:25.3.1"

Here it is how I use in my layout:

<android.support.v7.widget.RecyclerView
 app:lastAdapter = "@{viewModel.adapter}"
 ... />

I have already set the viewModel binding variable after inflating this layout. But whenever I run my codes, gradle always shows the following error

Error:(48, 34) Cannot find the setter for attribute 'app:lastAdapter' with parameter type error.NonExistentClass on android.support.v7.widget.RecyclerView. 

I can resolve this problem by removing any app:lastAdapter from my layouts then clean and rebuild, rewrite those app:lastAdapter attibutes again then rebuild project, everything works fine.

That is not a convenient solving.


Solution

  • As George said at the comment above "BindingAdapter takes a LastAdapter, but adapter appears to be a different type" and this is how the variable asigned:

    var adapter = LastAdapter(data, BR.item).map<MenuLinked, MenuItemBinding>(R.layout.menu_item)
    

    It seems DataBinding library can't determine implicit data type declaration from Kotlin, so it need to be defined explicitly:

    var adapter : LastAdapter = LastAdapter(data, BR.item).map<MenuLinked, MenuItemBinding>(R.layout.menu_item)