【问题标题】:Two-way databinding error: cannot resolve a setter for boolean property双向数据绑定错误:无法解析布尔属性的设置器
【发布时间】:2018-08-17 21:27:33
【问题描述】:

我正在尝试在复选框上使用 2 路数据绑定:

<layout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <data>
        <import type="android.view.View"/>
        <variable
            name="viewModel"
      type="com.epsilon.startbodyweight.selectorActivity.SelectorViewModel"/>
        <variable
            name="index"
            type="int" />
    </data>

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <CheckBox
            android:id="@+id/cb_active_exer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:checked="@={viewModel.exerciseList[index].isActive}" />

这给了我以下错误:

数据绑定错误消息:无法反转表达式 viewModelExerciseListIndex.isActive():双向绑定无法解析布尔属性“isActive”的设置器

我有以下课程:

@Entity
class ExerciseEntity {
    var exerciseName: String = ""
    @PrimaryKey
    var exerciseNum: Int = 0
    var progressionName: String = ""
    var progressionNumber: Int = 0
    var set1Reps: Int = 0
    var set2Reps: Int = 0
    var set3Reps: Int = 0
    var setTime: Int = 0
    var isTimedExercise: Boolean = false
    var numAttempts: Int = 0
    var isActive: Boolean = true

    // Do not store the following items in our DB
    @Ignore var allProgressions: ArrayList<String> = ArrayList()
    @Ignore var nextSet1Reps: Int = 0
    @Ignore var nextSet2Reps: Int = 0
    @Ignore var nextSet3Reps: Int = 0
    @Ignore var nextSetTime: Int = 0
    @Ignore var nextNumAttempts: Int = 0
    @Ignore var nextProgressionName: String = ""
    @Ignore var nextProgressionNumber: Int = 0
    @Ignore var isModified: Boolean = false
    @Ignore var exerMessage: String = ""
    @Ignore var isSet1Complete: Boolean = false
    @Ignore var isSet2Complete: Boolean = false
    @Ignore var isSet3Complete: Boolean = false
    @Ignore var isSetTimeComplete: Boolean = false
}

这是 SelectorViewModel,根据要求:

SelectorViewModel:

class SelectorViewModel : ViewModel() {
    private val mExerciseList = MutableLiveData<ArrayList<ExerciseEntity>>()
    val exerciseList : LiveData<ArrayList<ExerciseEntity>>
        get() = mExerciseList

    init {
        mExerciseList.value = ArrayList()
    }

    fun populateExerciseListFromDB(myDBExercises: List<ExerciseEntity>) {
        // .... Load our exer list from DB ... //


        mExerciseList.value?.addAll(myDBExercises)
        // Use the "setValue" method to notify observers of change
        mExerciseList.value = mExerciseList.value
    }

    private fun updateRepsInExercise(exercise: ExerciseEntity, reps1: Int, reps2: Int, reps3: Int) {
        exercise.set1Reps = reps1
        exercise.set2Reps = reps2
        exercise.set3Reps = reps3

        mExerciseList.value = mExerciseList.value
    }

    fun incrementSet(exercise: ExerciseEntity, smallIncrement: Boolean) {
            // ... Compute the new reps ... //
            updateRepsInExercise(exercise, numReps1, numReps2, numReps3)
    }
}

请注意:这不是与 this question 的重复,它指的是 Android Studio 2.2 中的一个错误,并且正在使用 Java。我在 Kotlin 中使用 3.2 版。

我尝试为函数 isActive 手动创建一个 setter,但它抱怨已经为布尔属性定义了一个 setter。

【问题讨论】:

  • 分享你的类 SelectorViewModel
  • @KrishnaSharma 添加了!
  • 一切看起来都不错,除了 viewModelExerciseListIndex
  • @KrishnaSharma 那么为什么你认为我会收到这个错误?
  • 找到原因了吗??如果不共享github中的代码库,我也可以调试

标签: android data-binding


【解决方案1】:

在编写绑定表达式时,请仔细检查自动完成中可用的属性。以 is 开头的 Kotlin Booleans 在没有它的绑定表达式语言中显示。

例如,考虑一个具有名为isActive 的属性的 Kotlin 数据类。

data class Exercise(var isActive: Boolean)

在绑定表达式中,该属性将显示为active,而不是isActive

<CheckBox
  android:checked="@={viewModel.exercise.active}" />

【讨论】:

    【解决方案2】:

    为ExerciseEntity类的isActive属性添加@JvmField注解

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 2014-02-21
      相关资源
      最近更新 更多