
[英]Android data binding Two-way binding cannot resolve a setter for boolean property
[英]Two-way databinding error: cannot resolve a setter for boolean property
我正在尝试在复选框上使用 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”的 setter
我有以下课程:
@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,根据请求:
选择器视图模型:
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)
}
}
请注意:这不是重复的,因为这个问题是指 Android Studio 2.2 中的一个错误并且正在使用 Java。 我在 Kotlin 中使用 3.2 版。
我试图为函数 isActive 手动创建一个 setter,但它抱怨已经为 boolean 属性定义了一个 setter。
给ExciseEntity类的isActive属性添加@JvmField注解
编写绑定表达式时,请仔细检查自动完成中可用的属性。 科特林Boolean
的起步与is
在没有它的绑定表达式语言显示。
例如,考虑一个具有名为isActive
的属性的 Kotlin 数据类。
data class Exercise(var isActive: Boolean)
在绑定表达式中,此属性将显示为active
,而不是isActive
。
<CheckBox
android:checked="@={viewModel.exercise.active}" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.