Search code examples
androidkotlinmvvmbindingseekbar

Textview and Seekbar are bound to the same property, but the textview doesn´t show the updated value


I´ve created a viemodel with a MutableLiveData

val duration = MutableLiveData(2)

In my Activity in set the Databinding

binding = DataBindingUtil.setContentView(this, R.layout.activity_ticket)
    binding.viewmodel = this.viewModel

Here is my Layout

<data>
    <variable
            name="viewmodel"
            type="com.text.viewmodel.TicketViewModel"/>
</data>
 ...
  <TextView
            android:id="@+id/textview_duration"
            android:layout_margin="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{String.valueOf(viewmodel.duration)}"
    />
    <SeekBar
            android:id="@+id/seekBar_duration"
            android:max="8"
            android:min="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:progress="@={viewmodel.duration}"
    />

When the user changed the seekbar, then the value in the ViewModel is updated correctly, but the text view does not refresh itself.


Solution

  • I found the solution by myself.

    I forgot to set the the activity as lifecycleowner

    binding = DataBindingUtil.setContentView<ActivityTicketBinding>(this, R.layout.activity_ticket)
    binding.lifecycleOwner = this
    binding.viewmodel = this.viewModel