Не удалось вызвать метод наблюдателя Android Привязка данных - PullRequest
0 голосов
/ 28 января 2020

Когда я добавил isEnabled в свой файл ресурсов макета

 <androidx.coordinatorlayout.widget.CoordinatorLayout
                style="@style/TopLevelLayout">
                <Spinner
                    selectedItem="@={addLocationViewModel.lsgiType}"
                    isEnabled="@{addLocationViewModel.village.equals(@string/CHOOSE_VALUE) ? false : true}"
                    android:id="@+id/lsgiType"
                    style="@style/Spinner"
                    android:entries="@{addLocationViewModel.lsgiTypes}"/>

                <TextView
                    android:id="@+id/lsgiTypeTextView"
                    style="@style/SpinnerTextView"
                    android:text="LSGI Type"/>
            </androidx.coordinatorlayout.widget.CoordinatorLayout>

, после этого я получаю ошибку Failed to call observer method

вот мой ViewModel init код

// observe the district spinner and change taluk spinner state
    district.observeForever {
        if(it == CHOOSE_VALUE){
            taluks.value = listOf("--- Choose ---")
            taluk.value = CHOOSE_VALUE
        }else{
            taluks.value = localBodiesRepository.getTaluksNameByDistrict(district.value!!).let {
                    it1 -> listOf("--- Choose ---").plus(it1)
            }
            taluk.value = taluks.value?.get(0)
        }
    }

    // observe the taluk spinner and change village spinner state
    taluk.observeForever {
        if(it == CHOOSE_VALUE){
            villages.value = listOf("--- Choose ---")
            village.value = CHOOSE_VALUE
        }else{
            villages.value = localBodiesRepository.getVillagesNameByTaluk(taluk.value!!).let {
                    it1 -> listOf("--- Choose ---").plus(it1)
            }
            village.value = villages.value?.get(0)
        }
    }

    // observe the village spinner and change lsgi spinner state
    village.observeForever {
        if(it == CHOOSE_VALUE){
            lsgiTypes.value = listOf("--- Choose ---")
    //                lsgiTypeEnabled.value = DISABLED
            lsgiType.value = CHOOSE_VALUE
        }else{
            lsgiTypes.value = listOf("--- Choose ---").plus(localBodiesRepository.getLSGITypesName())
            if(formEditEnabled.value!!) {
    //                    lsgiTypeEnabled.value = ENABLED
            }else{
    //                    lsgiTypeEnabled.value = DISABLED
            }
        }
    }

    // observe the lsgi spinner and change lsgi spinner state
    lsgiType.observeForever {
        if(it == CHOOSE_VALUE){
            lsgis.value = listOf("--- Choose ---")
            lsgiEnabled.value = DISABLED
            lsgi.value = CHOOSE_VALUE
        }else{
            lsgis.value =localBodiesRepository.getLSGIsNamesUnderDistrictAndLSGIType(
                district.value!!,
                lsgiType.value!!
            ).let {
                    it1 -> listOf("--- Choose ---").plus(it1)
            }
            lsgi.value = lsgis.value?.get(0)
            if(formEditEnabled.value!!) {
                lsgiEnabled.value = ENABLED
            }else{
                lsgiEnabled.value = DISABLED
            }
        }
    }

    // observe ownerOfTheLand spinner
    ownerOfTheLand.observeForever { it ->
        when(it){
            "--- Choose ---","LSGI", "Approved auto/taxi stand" ,"Private" ->{
                ownerOfTheLandEditTextVisibility.value = View.GONE
            }
            "KSEB" ->{
                ownerOfTheLandEditTextVisibility.value = View.VISIBLE
                ownerOfTheLandEditTextHint.value = "Name Of Office"
            }
            "Kerala State Government", "Central Government"  ->{
                ownerOfTheLandEditTextVisibility.value = View.VISIBLE
                ownerOfTheLandEditTextHint.value = "Name Of Department"
            }
            "State PSU", "Central PSU" ->{
                ownerOfTheLandEditTextVisibility.value = View.VISIBLE
                ownerOfTheLandEditTextHint.value = "Name Of PSU"
            }
            "Autonomous Institution" ->{
                ownerOfTheLandEditTextVisibility.value = View.VISIBLE
                ownerOfTheLandEditTextHint.value = "Name Of Autonomous Institution"
            }
            "Others" ->{
                ownerOfTheLandEditTextVisibility.value = View.VISIBLE
                ownerOfTheLandEditTextHint.value = "Specify"
            }else ->{
                ownerOfTheLandEditTextVisibility.value = View.GONE
                ownerOfTheLandEditTextHint.value = "Specify"
            }
        }
    }

    // observe owner of the lands privates selection spinner
    ownerOfTheLandPrivate.observeForever {
        if(it == "Others"){
            ownerOfTheLandEditTextVisibility.value = View.VISIBLE
            ownerOfTheLandEditTextHint.value = "Please Specify"
        }else{
            if(ownerOfTheLand.value == "Private") {
                ownerOfTheLandEditTextVisibility.value = View.GONE
            }
        }
    }

    // observe frontage photo
    frontagePhoto.observeForever { if(it == null){ photoUri = "" } }

    editSwitchChecked.observeForever {
            if(location.isInDraft) {
                if (it == true) {
                    this@AddLocationViewModel.location =
                        location.id?.let { it1 -> locationRepository.getLocationById(it1)}!!
                    submitButtonText.value = "Save"
                    lsgiEnabled.value = true
      //                        lsgiTypeEnabled.value = true
                    formEditEnabled.value = true
                } else {
                    formEditEnabled.value = false
                    lsgiEnabled.value = false
      //                        lsgiTypeEnabled.value = false
                    submitButtonText.value = "Submit"
                    this@AddLocationViewModel.location =
                        location.id?.let { it1 -> locationRepository.getLocationById(it1) }!!
                }
            }
    }

    if(submitButtonText.value == "Save To Draft"){
        getLocationCoordinate()
    }
...