LiveData and ViewModel does not update textview

ashishdhiman2007 picture ashishdhiman2007 · May 7, 2018 · Viewed 9.3k times · Source

I am having a small issue, when i comment out the binding.setData(dataContainer); in onChanged while observing Livedata in my Activity, I am not able to update the UI, when I uncomment it, the UI is updated. Please guide and do a little code review if you feel the need.

I am having a Runnable that runs repeatedly after every x seconds. This is made Livedata which I observe in my Activity.

Thanks.

public class MainActivity extends AppCompatActivity {

  TextOnScreen dataContainer;
  ActivityMainBinding binding;
  ViewModelClass modelClass;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    modelClass = ViewModelProviders.of(this).get(ViewModelClass.class);
    binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    dataContainer = new TextOnScreen("hello");
    binding.setData(dataContainer);

    modelClass.getLiveData().observe(this, new Observer<String>() {
      @Override
      public void onChanged(String s) {
        Log.e("TAG", "--onChanged--" + s);
        dataContainer.onscreen.set(s);

        // this line allows the textview to update
        // other wise no change on UI is seen
        binding.setData(dataContainer);
      }
    });
  }
}


public class TextOnScreen {

  public final ObservableField<String> onscreen=new ObservableField<>();

  public TextOnScreen(String t) {
    onscreen.set(t);
  }

  public String getOnscreen() {
    return onscreen.get();
  }

  public void setOnscreen(String onscreen) {
    this.onscreen.set(onscreen);
  }
}


    public class ViewModelClass extends ViewModel{

      private MutableLiveData<String> liveData;

      public ViewModelClass() {
        ModelClass modelClass = new ModelClass();
        liveData= modelClass.generateName();
      }

      public LiveData<String> getLiveData() {
        return liveData;
      }
    }


public class ModelClass {

  MutableLiveData<String > data =new MutableLiveData<>();


  MutableLiveData<String> generateName(){
    final android.os.Handler handler=new android.os.Handler();
    Runnable runnable=new Runnable() {
      @Override
      public void run() {

        data.setValue(data.getValue()+"*");
        handler.postDelayed(this,1000);
      }
    };
    handler.postDelayed(runnable,2000);
    return data;
  }

}

<?xml version="1.0" encoding="utf-8"?>
<layout>
  <data>
    <variable
      name="data"
      type="dsfa.drish.com.livedatasample.TextOnScreen"/>
  </data>
<android.support.constraint.ConstraintLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity">

  <TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@{data.onscreen}"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>
</layout>

Answer

Flovettee picture Flovettee · Jan 19, 2020

For me it was just missing:

binding.setLifecycleOwner(this);