Solution 1 :
Find out the solution:
The most important part , add the below line to your Activity/Fragment:
// Required to update UI with LiveData
dataBinding.setLifecycleOwner(this);
Problem :
I have implemented two-way data-binding but it is not working as expected.
So here is my XML snippet :
<EditText
android_id="@+id/name_text"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_layout_margin="15dp"
android_layout_marginBottom="5dp"
android_ems="10"
android_text="@={taskViewModel.taskName}"
android_hint="Task name"
android_inputType="textPersonName"
android_textStyle="bold" />
<TextView
android_id="@+id/date_text"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_layout_margin="15dp"
android_layout_marginTop="5dp"
android_layout_marginBottom="5dp"
android_text="@={taskViewModel.taskDate}"
android_ems="10"
android_drawablePadding="5dp"
android_onClick="@{() -> listener.showDatePicker()}"
android_drawableRight="@drawable/ic_date"
android_hint="Pick a schedule date"
android_inputType="textPersonName"
android_textStyle="bold" />
<TextView
android_id="@+id/time_text"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_layout_margin="15dp"
android_layout_marginTop="5dp"
android_layout_marginBottom="5dp"
android_text="@={taskViewModel.taskTime}"
android_drawablePadding="5dp"
android_ems="10"
android_onClick="@{() -> listener.showTimePicker()}"
android_drawableRight="@drawable/ic_time"
android_hint="Pick a schedule time"
android_inputType="textPersonName"
android_textStyle="bold" />
Basically I’m trying to implement two-way data binding for the above widgets.
My ViewModel:
class TaskViewModel(private val repository: TaskRepository) : ViewModel() {
val taskName = MutableLiveData<String>()
val taskDate = MutableLiveData<String>()
val taskTime = MutableLiveData<String>()
/**
* Launching a new coroutine to insert the data in a non-blocking way
*/
fun insertTask() = viewModelScope.launch(Dispatchers.IO) {
val startDate = Calendar.getInstance().time
val scheduledDate = convertStringtoCalendar(taskDate.value, taskTime.value).time
val diffInTime = Util.calculateDateDifference(startDate , scheduledDate)
//User selected a proper date
if(diffInTime > 0) {
var newTask = Task(
taskName = taskName.value,
insertDate = Calendar.getInstance(),
scheduleDate = convertStringtoCalendar(taskDate.value, taskTime.value)
)
val insertRowId = repository.insert(newTask)
if (insertRowId > -1) {
statusMessage.postValue("Task Inserted Successfully $insertRowId")
newWordMutableLiveData.postValue(newTask)
taskName.postValue(null)
taskDate.postValue(null)
taskTime.postValue(null)
} else {
statusMessage.postValue("Error Occured")
newWordMutableLiveData.postValue(null)
}
insertRowIdMutableLiveData.postValue(insertRowId)
Log.d("InsertId", insertRowId.toString())
}else{
statusMessage.postValue("Scheduled time is less than current time.")
}
}
}
Here you can see , I am setting the MutableLiveData to null using :
taskName.postValue(null)
taskDate.postValue(null)
taskTime.postValue(null)
The problem is, however, the mutableLiveData values is cleared but the textViews and EditText
s text in the layout is not cleared. I need to clear them. As I am setting their corresponding MutableLiveData values to null, so they are supposed to get cleared on the insertTask() method call. But that is not the case. What wrong I am doing?
Comments
Comment posted by Mohsen
try an empty string instead of null I mean “”
Comment posted by kgandroid
sorry not working…