Cannot find the setter for attribute in Data binding
Asked Answered
D

4

15

I am using LiveData, DataBinding, and Kotlin in my application.

I defined a Binding Adapter for a RecyclerView like this:

   class MainListBindings {

    private val TAG = "TasksListBindings"

    companion object {

        @JvmStatic
        @SuppressWarnings("unchecked")
        @BindingAdapter("main_items")
        fun setItems(recyclerView: RecyclerView, items: MutableLiveData<List<Homeitem>>? = null) {

            val adapter: RecyclerMainAdapter = recyclerView.adapter as RecyclerMainAdapter
            //prevent use of null list
            items?.let {

                adapter.swapData(items)
            }
        }

    }
}

and my reyclerView in XML assigned to this bindingAdapter like this:

  <android.support.v7.widget.RecyclerView
      android:id="@+id/main_recycler"
      app:main_items="@{viewmodel.items}"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>

And in my ViewModel, I used

val items = MutableLiveData<List<Homeitem>>()

to create the list.

But I am getting this error at building Application:

Found data binding errors.
****/ data binding error ****msg:Cannot find the setter for attribute 'app:main_items' with parameter type android.arch.lifecycle.MutableLiveData<java.util.List<project.data.pojo.Homeitem>> on android.support.v7.widget.RecyclerView. file:/home/user/Yara/bazinama_mag/bazinama/app/src/main/res/layout/fragment_main.xml loc:22:24 - 22:38 ****\ data binding error ****
Dougall answered 20/8, 2018 at 8:20 Comment(9)
did you declare attribute for the property main_items for RecyclerView? in file Res\values\attrs.xmlTimber
No, I didn't do thatDougall
have a look at this SO post https://mcmap.net/q/57604/-defining-custom-attrsTimber
@Timber this is pretty oldschool and not really necessary in this case, since Ehsan can use BindingAdaptersKenspeckle
okay so then the only method missing within Homeitem is public void setMainItems(...); ?Timber
Hello Ehsan, did you try removing leading app: namespace in at @BindingAdapter("app:main_items")?Reduction
@KasimRangwala Hello, Yes I tried that but didn't work.Dougall
have you found out the solution?, I'm facing the same problemExplosion
@Andrea I Post my solution. Hope that will help you.Dougall
D
47

There might be different reasons for this error but in my case, the problem raised up because I didn't add apply plugin: 'kotlin-kapt' And apply plugin: 'kotlin-android-extensions' in my Gradle.

After adding these plugins you have to replaced your annotationProcessors with kapt.

After that, every thing might be going well.

Dougall answered 29/10, 2018 at 6:34 Comment(4)
How do you replace the "annotationProcessors" Is that in a file or something?Copeck
in your gradle file just replace annotationProcessors with kaptDougall
I really missed this in the docs. I was looking where my code was wrong while it was just the kotlin-kapt all alongVentris
Thank you so much, I tried everything, was on this for about 2 hours. :DPindus
M
6

Binding Adapter

    @JvmStatic
    @BindingAdapter("main_items")
    fun setItems(recyclerView: RecyclerView, items: MutableLiveData<List<String>>) {

    }

Model

 class User : ViewModel(){
        lateinit var list: MutableLiveData<List<String>>
    }

Bind Data

 val items = ViewModelProviders.of(this@FragmentName)
                .get(RecyclerViewHelper.User::class.java)
    mBinding!!.user = items

Layout

<layout>
     <data>
        <variable
            name="user"
            type="com.XXX.view.recylerview.RecyclerViewHelper.User" />
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/helper_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layoutManager="android.support.v7.widget.LinearLayoutManager"
            app:main_items="@{user.list}" />

    </RelativeLayout>

</layout>
Mehalick answered 20/8, 2018 at 10:1 Comment(1)
it doesn't work. Also I update my question with this changesDougall
K
1

Check if you have any space after the @BindingAdapter value like this:

@BindingAdapter("sleepDurationFormatted ")
                                       ^
                                       |
                                      here

Took me 2hours to find out.

Karyosome answered 19/8, 2020 at 16:12 Comment(1)
I fixed mine. It's a typoPartlet
O
0

2023 here. Have to refactor a 2021 project.

My problem was that I wasn’t actually specifying exactly the type from the VM.

XML:

<TextView
        android:id="@+id/location"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="12dp"
        android:ellipsize="end"
        android:lines="1"
        android:maxLines="1"
        android:text="@{viewModel.location}”

And the VM:

    val location = session.location?.toLongString()

Fixing this with:

    val location: CharSequence = session.location?.toLongString() ?:""
Octave answered 9/11, 2023 at 7:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.