Android 双向数据绑定屏幕或定位更改值
我正在使用双向数据绑定。 我的房间数据库中有一些数据,这取决于用户在edittext中写入的内容,在textview中显示该值 当我旋转屏幕时,值会更新。一开始它是空的,然后又回到原来的样子。我知道这是活动生命周期的正常行为 所以我想知道解决这个问题的最好方法 提前谢谢 编辑文本:Android 双向数据绑定屏幕或定位更改值,android,android-room,android-databinding,android-livedata,Android,Android Room,Android Databinding,Android Livedata,我正在使用双向数据绑定。 我的房间数据库中有一些数据,这取决于用户在edittext中写入的内容,在textview中显示该值 当我旋转屏幕时,值会更新。一开始它是空的,然后又回到原来的样子。我知道这是活动生命周期的正常行为 所以我想知道解决这个问题的最好方法 提前谢谢 编辑文本: <EditText android:layout_width="0dp" android:layout_height="wrap_content"
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/edtOddelek"
android:text="@={viewModel.loginOddelek}"
android:layout_marginEnd="8dp" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/textView3"
app:layout_constraintBottom_toBottomOf="@+id/textView3"
app:layout_constraintStart_toStartOf="@+id/edtGeslo" style="@style/adoEditTextStyle"/>
文本视图:
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/txtOddelek"
android:layout_marginTop="8dp"
android:text="@{viewModel.oddelek}"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="12dp"
android:textColor="@color/orangeDark"
app:layout_constraintTop_toBottomOf="@+id/edtOddelek"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="12dp"/>
视图模型:
val loginOddelek = NonNullMutableLiveData<String>("")
val oddelek:LiveData<String> = Transformations.switchMap(loginOddelek){
settingsModel.getSifrPr(it)
}
val loginOddelek=NonNullMutableLiveData(“”)
val oddelek:LiveData=Transformations.switchMap(loginOddelek){
settingsModel.getSifrPr(it)
}
你能更具体地说明你想摆脱什么吗loginOddelek
noramlly不应受到屏幕旋转的影响,因此getSifrPr
也不会被调用。当屏幕旋转发生时,edittext的值变为“”(空字符串)。然后,价值又回到了过去的状态。这种情况是由于生命周期造成的。如果有运行时间较长的任务是问题所在。可以使用ContextChanged,但我真的希望所有代码都尽可能干净。你能更具体地说明你想去掉什么吗loginOddelek
noramlly不应受到屏幕旋转的影响,因此getSifrPr
也不会被调用。当屏幕旋转发生时,edittext的值变为“”(空字符串)。然后,价值又回到了过去的状态。这种情况是由于生命周期造成的。如果有运行时间较长的任务是问题所在。可以使用ContextChanged,但我真的希望所有代码都尽可能干净。