UI 在 android kotlin 中更新 viewModel 时未更新

UI is not updating while viewModel is updated in android kotlin

我遵循 MVVM 模式 Activity-->ViewModel ---> Repository 。存储库正在调用 api 并更新了 LiveData。 LiveData 的值也在 ViewModel 中更新,但未反映在 Activity 上。请指导我在哪里我失踪了,代码如下

Activity代码:

class LoginWithEmailActivity : AppCompatActivity() {

    private var loginViewModel: LoginViewModel? = null
    private var binding: ActivityLoginWithEmailBinding? = null
    private var btnLogin : Button? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
       
        loginViewModel = ViewModelProviders.of(this).get(LoginViewModel::class.java)

        binding = DataBindingUtil.setContentView(this@LoginWithEmailActivity, R.layout.activity_login_with_email)
        binding!!.setLifecycleOwner(this)
        binding!!.setLoginViewModel(loginViewModel)
        btnLogin = findViewById(R.id.btn_login)

        loginViewModel!!.servicesLiveData!!.observe(this, Observer<LoginDataModel?> { serviceSetterGetter ->

            val msg = serviceSetterGetter.success
            Toast.makeText(this@LoginWithEmailActivity, ""+msg, Toast.LENGTH_SHORT).show()
            Log.v("///LOGIN SUCCESS////",""+msg);
           
        })
        btnLogin!!.setOnClickListener {
            loginViewModel!!.getUser()

        }
}

ViewModel.kt

class LoginViewModel : ViewModel() {
    var servicesLiveData: MutableLiveData<LoginDataModel>? = MutableLiveData()

    fun getUser() {
        servicesLiveData = MainActivityRepository.getServicesApiCall()
    }
}

Repository.kt

object MainActivityRepository {

    val serviceSetterGetter = MutableLiveData<LoginDataModel>()

    fun getServicesApiCall(): MutableLiveData<LoginDataModel> {
        val params = JsonObject()
        params.addProperty("email", "xyz@gmail.com")
        val call: Call<LoginDataModel> = ApiClient.getClient.getPhotos(params)

        call.enqueue(object : Callback<LoginDataModel> {

            @RequiresApi(Build.VERSION_CODES.N)
            override fun onResponse(call: Call<LoginDataModel>?, response: Response<LoginDataModel>?) {
                if (response != null) {
                    val data = response.body()
                    serviceSetterGetter?.postValue(data);
                   
                }
            }

            override fun onFailure(call: Call<LoginDataModel>?, t: Throwable?) {
            }

        })
        return serviceSetterGetter
    }
}

您在onCreate中订阅了LiveData

loginViewModel!!.servicesLiveData!!.observe(this, Observer<LoginDataModel?> { serviceSetterGetter ->

            val msg = serviceSetterGetter.success
            Toast.makeText(this@LoginWithEmailActivity, ""+msg, Toast.LENGTH_SHORT).show()
            Log.v("///LOGIN SUCCESS////",""+msg);
           
        })

然后 getUser 创建了一个新的引用

fun getUser() {
    servicesLiveData = MainActivityRepository.getServicesApiCall()
}

您订阅的与getUser liveData不一致

如果你想保持你所拥有的大部分相同,你需要使用 MediatorLiveData

或者直接做

getUser().observe(this, Observer<LoginDataModel?> { serviceSetterGetter ->

    val msg = serviceSetterGetter.success
    Toast.makeText(this@LoginWithEmailActivity, ""+msg, Toast.LENGTH_SHORT).show()
    Log.v("///LOGIN SUCCESS////",""+msg);
           
})

fun getUser(): LiveData<LoginDataModel> {
    return MainActivityRepository.getServicesApiCall()
}