Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataBinding RecyclerView: Cannot find the setter for attribute 'app:items'

I got this error when I try to biding RecyclerView

Error:(15, 22) Cannot find the setter for attribute 'app:items' with parameter type android.databinding.ObservableArrayList<com.toong.databindingdemo.recycler.UserViewModel on android.support.v7.widget.RecyclerView.

Here is my code:

<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        <variable
            name="usersViewModel"
            type="com.toong.databindingdemo.recycler.UsersViewModel" />
    </data>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/activity_users_recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:items="@{usersViewModel.users}"
        />
</layout>

But in UsersViewModel I already have a public users array

package com.toong.databindingdemo.recycler;

import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.databinding.ObservableArrayList;

public class UsersViewModel extends BaseObservable{
    @Bindable
    public ObservableArrayList<UserViewModel> users;

    public UsersViewModel()
    {
        this.users = new ObservableArrayList<>();
    }
}

I have clean and rebuild project but it still not working. How can I fix this error?

like image 400
Linh Avatar asked Nov 03 '16 07:11

Linh


1 Answers

There is no property like app:items in RecyclerView. You need to create custom Binding method for that

@BindingAdapter("items")
public static void entries(RecyclerView recyclerView, String[] array) {
    //write your code to set RecyclerView adapter.
}

change type of array to your required datatype.

like image 130
Ravi Avatar answered Oct 14 '22 11:10

Ravi