代码之家  ›  专栏  ›  技术社区  ›  kgandroid m.qadhavi

双向数据绑定不适用于editText和TextView

  •  0
  • kgandroid m.qadhavi  · 技术社区  · 5 年前

    我已经实现了双向数据绑定,但它没有按预期工作。 所以这是我的 :

     <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" />
    

    基本上,我正在尝试为上述小部件实现双向数据绑定。

    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.")
            }
        }
    }
    

    您可以看到,我正在使用以下命令将MutableLiveData设置为null: taskName.postValue(空) taskDate.postValue(空) taskTime.postValue(空)

    然而,问题是,mutableLiveData值被清除,但textView s and EditText

    0 回复  |  直到 5 年前
        1
  •  0
  •   kgandroid m.qadhavi    5 年前

    找出解决方案:

    最重要的是,在活动/片段中添加以下行:

    // Required to update UI with LiveData
       dataBinding.setLifecycleOwner(this);