danPyk
danPyk

Reputation: 671

Binding adapter - cannot find setter for LiveData variable in recycler view

I'm trying to implement RecyclerView with GridLayoutManager and I'm stuck. I'm receiving error:


If a binding adapter provides the setter, check that the adapter is annotated correctly and that the parameter type matches.
Open File"

When I'm trying to compile. "Open file" provides me to xml recycler view declaration.

This is my BindingAdapter:

    @BindingAdapter("listData")
    fun bindRecyclerView(recyclerView: RecyclerView,
                         data: List<MarsPropertyData>?) {
        val adapter = recyclerView.adapter as PhotoGridAdapter
        adapter.submitList(data)
    }

How I'm calling the above:

      <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/photos_grid"
            android:clipToPadding="false"
            app:layoutManager=
                "androidx.recyclerview.widget.GridLayoutManager"
            app:spanCount="2"
            tools:itemCount="16"
            tools:listitem="@layout/grid_view_item"
            app:listData="@{viewModel.properties}"
            />

And this is my properties variable:

val properties: LiveData<List<MarsPropertyData>>

I'm understand that IDE complains about data types, my properties var it is LiveData<List<MarsPropertyData>> and inside BindingAdapter there's just normal List, but I saw example from google in which logic was made in same way and it worked fine.

Yes, I have added 'kotlin-kapt' plugin, I have another BindingAdapter which works fine.

Upvotes: 1

Views: 378

Answers (2)

Wasif Mujahid
Wasif Mujahid

Reputation: 1

Im too late to answer it, but may be it will help someone else.

The issue is at compile time the bindingAdapter method is not generated which compiler needs to call and set the list. To solve this you need to add one more annotation. which is @JvmStatic it will generate method at compile time and then your issue will be resolved. I shared the example below:

@JvmStatic
@BindingAdapter("listData")
fun bindRecyclerView(recyclerView: RecyclerView,
                     data: List<MarsPropertyData>?) {
    val adapter = recyclerView.adapter as PhotoGridAdapter
    adapter.submitList(data)
}

Upvotes: 0

Aminu Abubakar
Aminu Abubakar

Reputation: 11

I experienced a similar problem while working on a google codelab whose setup is very similar to yours. In my case, I inadvertently defined the "bindRecyclerView" bindingAdapter function within the curly braces of a previous bindingAdapter function. This resulted in the "bindRecyclerView" function being eclipsed by the previous function and hence its setter cannot be seen from other files. When I removed it from within the previous function, everything worked fine.

Upvotes: 1

Related Questions