@Bindable与方法错误 - 数据绑定相关联

时间:2017-12-11 14:11:18

标签: android

我正在使用数据绑定库使用属性setter和方法

更新TextView的可见性

的TextView:

<TextView
                    android:id="@+id/profileLblTtv"
                    android:layout_width="270dp"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"
                    android:text="@string/profile_photo_gr"
                    android:gravity="center"
                    android:layout_marginTop="15dp"
                    android:visibility="@{viewmodel.kalase(), default=gone}"
                    app:layout_constraintTop_toBottomOf="@+id/reqfldsTtv"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent" />

和可绑定的viewmodel方法'kalase':

@Bindable
public int kalase() {
    return userRole != null && userRole.getId() != 0 ? View.VISIBLE : View.GONE;
}

我收到以下错误:'与方法关联的@Bindable必须遵循JavaBeans约定kalase()'。谁能告诉我出了什么问题?

2 个答案:

答案 0 :(得分:0)

我认为您在扩展 BaseObservable 类的类中使用此方法。在此类中,您必须使用观察字段的方法

UserRole userRole;

public void setUserRole(UserRole userRole) {
    this.userRole = userRole;
    notifyPropertyChanged(BR.userRole);
}

@Bindable
public int getUserRole() {
    return userRole != null && userRole.getId() != 0 ? View.VISIBLE : View.GONE;
}

答案 1 :(得分:0)

一年半以后,我很可能迟到了聚会,但是...

该错误是由于您在函数名称上具有“ @Bindable”标记而导致的,该标记的名称不符合JavaBeans约定。为了使其确认,只需调用方法getKalase()(而不是kalase()),然后在布局文件中将其称为viewmodel.kalase(不带括号)