gpt4 book ai didi

android - 双向数据绑定(bind)错误 : cannot resolve a setter for boolean property

转载 作者:行者123 更新时间:2023-12-05 00:16:52 24 4
gpt4 key购买 nike

我正在尝试在复选框上使用双向数据绑定(bind):

<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}" />

这给了我以下错误:

数据绑定(bind)错误消息:无法反转表达式 viewModelExerciseListIndex.isActive():双向绑定(bind)无法解析 bool 属性“isActive”的 setter

我有以下类(class):

@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:

选择器 View 模型:

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,但它提示说已经为 bool 属性定义了一个 setter。

最佳答案

在编写绑定(bind)表达式时仔细检查自动完成中可用的属性。以 is 开头的 Kotlin Boolean 出现在没有它的绑定(bind)表达式语言中。

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

data class Exercise(var isActive: Boolean)

在绑定(bind)表达式中,此属性将显示为active,而不是isActive

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

关于android - 双向数据绑定(bind)错误 : cannot resolve a setter for boolean property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51903085/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com