gpt4 book ai didi

MVVM - 自定义模型的 MutableLiveData 没有通过数据绑定(bind)更新到 ViewModel 并且始终为空

转载 作者:行者123 更新时间:2023-12-05 00:50:54 25 4
gpt4 key购买 nike

摘要:

当我尝试向存储库发送自定义模型时,MutableLiveData 为空。我认为这是因为 MutableLiveData 的观察者。

请读到最后。

查看型号

class LoginViewModel @Inject constructor(
private val repository: MyRepository) : ViewModel() {


var loginModel: MutableLiveData<LoginModel>

init {
loginModel = MutableLiveData<LoginModel>()
}

fun loadUser(): LiveData<Response<Custom<Token>>> {

return repository.login(loginModel.value!!)
}
}

正如你在这里看到的,我有一个 MutableLiveData

我的 登录型号 是这样的
data class LoginModel(var user : String, var password : String)

使用数据绑定(bind)和与上面的 ViewModel 绑定(bind)来定义片段。

login_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.app.example.view.BaseActivity">

<data>
<variable
name="viewModel"
type="com.app.example.view.login.LoginViewModel" />

</data>

<android.support.constraint.ConstraintLayout
android:id="@+id/activityMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_login">

<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="80dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="80dp"
app:cardCornerRadius="7dp"
app:cardElevation="22dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:layout_marginTop="60dp"
android:text="@string/bienvenido_text"
android:textAllCaps="true"
android:textSize="20sp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:orientation="vertical">

<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<EditText
android:id="@+id/etEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:cursorVisible="true"
android:text="@{viewModel.loginModel.user}"
android:gravity="center|start|bottom"
android:hint="@string/email_text"
android:inputType="textEmailAddress"
android:maxLength="50"
android:paddingBottom="10dp"
android:textSize="18sp" />

</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true">

<android.support.design.widget.TextInputEditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="30dp"
android:cursorVisible="true"
android:gravity="center|start|bottom"
android:hint="@string/contrasenia_text"
android:text="@{viewModel.loginModel.password}"
android:inputType="textPassword"
android:maxLength="50"
android:paddingBottom="10dp"
android:textSize="18sp" />

</android.support.design.widget.TextInputLayout>

<Button
android:id="@+id/btnServerLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="15dp"
android:padding="10dp"
android:text="@string/ingresar_text"
android:textSize="18sp" />
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_marginBottom="40dp"
android:orientation="horizontal">

</LinearLayout>
</android.support.v7.widget.CardView>

</android.support.constraint.ConstraintLayout>

</layout>

使用数据绑定(bind),我已经实现了 之间的关系电子邮箱 和属性(property) 用户 模型 UserModel 并且我已经实现了 之间的关系电子密码 和属性(property) 密码

当用户点击 btnServer登录 , 登录片段 将执行以下代码
binding.btnServerLogin.setOnClickListener {
loginViewModel!!.loadUser().observe(this, Observer<Response<Custom<Token>>> { this.handleResponse(it) })
}

这是问题 .

kotlin.KotlinNullPointerException



有原因是因为loginModel.value!!为空
fun loadUser(): LiveData<Response<Custom<Token>>> {

return repository.login(loginModel.value!!)
}

MutableLiveData 的值始终为空。我虽然它会在您输入电子邮件或密码的 EditText 的同时发生变化。

这是我在其中初始化 ViewModel 的 LoginFragment OnActivityCreated 的代码
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
setHasOptionsMenu(true)
DeviceUtils.setTranslucentStatusBar(activity!!.window, R.color.colorPrimaryDark)

loginViewModel = ViewModelProviders.of(this, viewModelFactory)
.get(LoginViewModel::class.java)
binding.setLifecycleOwner(this)
binding.viewModel = loginViewModel


binding.btnServerLogin.setOnClickListener {
mPostsViewModel!!.loadUser().observe(this, Observer<Response<RespuestaModel<JwtTokenModel>>> { this.handleResponse(it) })
}

return
}

我究竟做错了什么? 我需要添加特定的观察者吗?

谢谢

最佳答案

感谢@communistWatermelon,我能够解决一部分问题,但这还不够。这是完整的解决方案:
一方面,我们必须初始化 MutableLiveData 的 value 属性,正如@communistWatermelon 所说:

var loginModel: MutableLiveData<LoginModel> = MutableLiveData()

init {
loginModel.value = LoginModel()
}
另一方面,我们需要在布局中正确设置绑定(bind)

In the case of @{variable.field} the binder will generate a piece of code to get the value by calling variable.getField(). Note that the binder implicitly prefix the “get” word if it is not provided. So @{variable.getField()} is an explicit valid option.

In the case of @={variable.field} the binder will create the same code as before, but with additional code for getting the value from the View and setting it back to your object


看完之后,我们需要改变绑定(bind)的方式
android:text="@{viewModel.loginModel.password}"
android:text="@{viewModel.loginModel.user}"
android:text="@={viewModel.loginModel.password}"
android:text="@={viewModel.loginModel.user}"
编辑
对于那些没有使用上述解释解决的人,请尝试使用以下行在您的片段中注册生命周期所有者:
binding.setLifecycleOwner(this)
好编码!

关于MVVM - 自定义模型的 MutableLiveData 没有通过数据绑定(bind)更新到 ViewModel 并且始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52893598/

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