williamyyu/SimpleRatingBar

data binding - two way binding error

nurjan84 opened this issue · 5 comments

android:rating="@={estimateViewModel.rating}"

[kapt] An exception occurred: android.databinding.tool.util.LoggedErrorException: Found data binding errors. ****/ data binding error ****msg:Cannot find the getter for attribute 'android:rating' with value type java.lang.Float on com.willy.ratingbar.ScaleRatingBar.

Hello @nurjan84 ,

Maybe you should use app:srb_rating, not android:rating
I haven't try it on data binding, so if there still have problem, please tell me.
Thank you.

Hello! Same error with app:srb_rating

[kapt] An exception occurred: android.databinding.tool.util.LoggedErrorException: Found data binding errors. ****/ data binding error ****msg:Cannot find the getter for attribute 'app:srb_rating' with value type java.lang.Float on com.willy.ratingbar.ScaleRatingBar.

Hi,
Sorry that I've been a little bit busy these days,
I'll try to find out the problem when I have more free time.
Thank you for issue this problem!

add ScaleRatingBarBindingAdapter.java to your project
xml use app:rating="@={estimateViewModel.rating}"


import androidx.databinding.BindingAdapter;
import androidx.databinding.InverseBindingListener;
import androidx.databinding.InverseBindingMethod;
import androidx.databinding.InverseBindingMethods;

import com.willy.ratingbar.BaseRatingBar;
import com.willy.ratingbar.ScaleRatingBar;

@InverseBindingMethods({
        @InverseBindingMethod(type = ScaleRatingBar.class, attribute = "rating"),
})
public class ScaleRatingBarBindingAdapter {
    @BindingAdapter("android:rating")
    public static void setRating(RatingBar view, float rating) {
        if (view.getRating() != rating) {
            view.setRating(rating);
        }
    }
    @BindingAdapter(value = {"onRatingChanged", "ratingAttrChanged"},
            requireAll = false)
    public static void setListeners(ScaleRatingBar view, final ScaleRatingBar.OnRatingChangeListener listener,
            final InverseBindingListener ratingChange) {
        if (ratingChange == null) {
            view.setOnRatingChangeListener(listener);
        } else {
            view.setOnRatingChangeListener(new BaseRatingBar.OnRatingChangeListener() {
                @Override
                public void onRatingChange(BaseRatingBar ratingBar, float rating, boolean fromUser) {
                    if (listener != null) {
                        listener.onRatingChange(ratingBar, rating, fromUser);
                    }
                    ratingChange.onChange();
                }
            });
        }
    }
}

Also have the issue with the following: app:srb_rating="@{rating}"

Cannot find a setter for <com.willy.ratingbar.BaseRatingBar app:srb_rating> that accepts parameter type 'java.lang.Float'