I am trying to update an EditText from View page in Kotlin using mvvm. But when ViewModel is initialised the default value is binding but when i change the value from EditText onClick listener its not updating the UI.
xml
<EditText
android:id="@+id/itemsDatePicker"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="@={itemviewmodel.selectedDateText}"
android:onClick="@{itemviewmodel.datePickerOnClick}"
android:hint="Select Date"
tools:ignore="AutoFill,TextFields" />
View Model
fun datePickerOnClick(view: View) {
showDatePicker.value=true
}
View
vm.showDatePicker.observe(this,androidx.lifecycle.Observer{ s->
val myCalendar: Calendar = Calendar.getInstance()
val date =
DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth -> // TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year)
myCalendar.set(Calendar.MONTH, monthOfYear)
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth)
//This place should update the ui with selected date but not working as expected
vm.selectedDateText.value =DateHelper.getTodayDate(myCalendar.getTime())
}
DatePickerDialog(
this, date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)
).show()
})
Any help to overcome this problem is much appreciated. Thanks in advance. :)
If you are talking about dataBinding, and i assume that you already have it in your dependencies.
Then...
Check if you contained you xml mainLayout with <layout></layout>
Check if you you have made an instance of that layout on top of onCreate in your mainActivity. For ex: private lateinit var mainBinding: ActivityMainBinding
Use that instance/variable to also get the editText for ex: mainBinding.itemsDatePicker
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.