0

I'm new to android development so this may be simple but I just cant see what is wrong.

I have a data class (which is actually a room entity if that matters) , for example :

@Entity(...)
data class MyDataClass (
   ...
   var dataType:Int=0,
   ...
)

In viewmodel I have

val data = MutableLiveData<MyDataClass>()

In the viewmodel init block this is initialised

In my activity layout xml I have various views to allow editing of the data class

There is a spinner in the layout which edits the dataType :

<Spinner
   ...
   android:selectedItemPosition="@={viewmodel.data.dataType}"
/>

So far it works.

I then have a view in the layout

<LinearLayout
   ...
   app:hideIfZero="@{viewmodel.data.dataType}"
/>

where the hideIfZero binding adapter is

@BindingAdapter("app:hideIfZero")
fun hideIfZero(view: View, number:Int) {
    view.visibility = if (number == 0) View.GONE else View.VISIBLE
}

When I run the app, I can edit all of my fields including the dataType via the spinner. However when I change the spinner value the visibility of the LinearLayout is not changed.

What am I missing ?

Do I need to somehow tell the activity to refresh the complete layout. Any examples I've found seem to imply this should happen automatically.

2
  • You're binding to the dataType member, which is just an Int. From the databinding documentation: "Any plain-old object can be used for data binding, but modifying the object doesn't automatically cause the UI to update".
    – Michael
    Apr 28, 2020 at 10:11
  • Michael - so is there a way to mark (annotate) dataType so that modification does cause a UI update ?
    – john4663
    Apr 28, 2020 at 14:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.