【发布时间】:2018-08-19 14:10:43
【问题描述】:
我有以下问题:
对于我当前的 Android 应用程序项目,我使用 Realm 作为数据库,并且每次我的某个文本字段随赋予文本视图的值发生变化时,我都会尝试更新字符串值。但是,问题是,每当对象的值发生更改时,文本字段的值就会与先前设置的字符一起出现并变回。我更改的项目也在我的视图中。有任何想法吗? 这是我的代码
布局
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/entry_detail_form_8_til_text_field_container"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
app:hintEnabled="true"
app:layout_constraintTop_toBottomOf="@id/entry_detail_form_8_title"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<EditText
android:id="@+id/entry_detail_form_8_et_text_field"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
style="@style/Widget.MaterialComponents.TextInputEditText.OutlinedBox"/>
</com.google.android.material.textfield.TextInputLayout>
和逻辑
holder.etTextArea?.addTextChangedListener(object : TextWatcher{
override fun afterTextChanged(p0: Editable?) {
}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
realm.executeTransaction {
holder.fieldObject?.value = p0.toString()
}
}
})
编辑:以下操作未能解决问题:
- 将更改领域对象逻辑提取到适配器外部的回调中
- 将逻辑移入 afterTextChanged
【问题讨论】:
-
试试
afterTextChanged而不是onTextChanged -
@LordRaydenMK 同样的结果
-
您是否将文本设置为“holder.etTextArea”?任何地方。
-
@R7G 不,奇怪的是,该错误仅在我将“field.value = value”行添加到逻辑时发生,没有它,什么都不会被触发两次