如何解决视图模型只触发一次
我有这个ViewModel
class RegisterShopViewModel : ViewModel() {
private val client by lazy { WebServicesApi.getClient }
val isShopRegisterSuccessful = MutableLiveData<Pair<Boolean,List<String>>>()
fun registerShop(shopModel: ShopModel): LiveData<Pair<Boolean,List<String>>> {
client.registerShop(shopModel)
.enqueue(object : Callback<RegisterResponseModel> {
override fun onFailure(call: Call<RegisterResponseModel>,t: Throwable) {
isShopRegisterSuccessful.value = Pair(false,listOf(t.message!!))
}
override fun onResponse(
call: Call<RegisterResponseModel>,response: Response<RegisterResponseModel>
) {
response.body()?.let {
isShopRegisterSuccessful.value = Pair(it.isSuccess,it.errorsList)
}
}
})
return isShopRegisterSuccessful
}
这是我的活动
class RegisterShopActivity : AppCompatActivity() {
private val TAG = RegisterShopActivity::class.simpleName
private val registerShopViewModel: RegisterShopViewModel by lazy {
ViewModelProviders.of(this).get(RegisterShopViewModel::class.java)
}
private fun registerButtonClicked() {
val email = txtShopEmail.text.toString()
val password = txtShopPassword.text.toString()
val confirmPassword = txtShopConfirmPassword.text.toString()
val name = txtShopName.text.toString()
val shopModel = ShopModel(email,password,confirmPassword,name)
observe(registerShopViewModel.register(shopModel),::registerShop)
}
private fun registerShop(pair: Pair<Boolean,List<String>>) {
if (pair.first) Toast.makeText(this,"Registered Successfully",Toast.LENGTH_SHORT).show()
else Toast.makeText(this,"error ",Toast.LENGTH_SHORT).show()
}
问题是 ViewModel
给了我第一个值,如果我更改了输入,它仍然给我旧值。我在这里做错了什么?
还有一个问题。当我在 registerShop()
方法上放置断点时。它永远不会停在那里
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。