3

I am trying to use 2-way databinding on a checkbox:

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

This gives me the following error:

databinding error msg:The expression viewModelExerciseListIndex.isActive() cannot be inverted: Two-way binding cannot resolve a setter for boolean property 'isActive'

I have the following 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
}

Here is the SelectorViewModel, as per request:

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)
    }
}

Please note: this is not a duplicate as this question which is referring to a bug in Android Studio 2.2 and is using Java. I am using version 3.2 with Kotlin.

I tried to manually create a setter for the function isActive but it complained that there was already a setter defined for the boolean property.

Lanaru
  • 9,421
  • 7
  • 38
  • 64

2 Answers2

7

Double check the properties available in autocomplete when writing your binding expression. Kotlin Booleans beginning with is show up in the binding expression language without it.

For example, consider a Kotlin data class with a property called isActive.

data class Exercise(var isActive: Boolean)

In the binding expression, this property will appear as active, not isActive.

<CheckBox
  android:checked="@={viewModel.exercise.active}" />
M. Palsich
  • 1,748
  • 13
  • 18
1

Add @JvmField annotation to the isActive property of the ExerciseEntity class

Sundaravel
  • 464
  • 5
  • 16