0

I'm trying the LiveData and ViewModel architecture. I have a simple counter in the ViewModel which is observed on MainActivity. I am able to confirm that the data is acquired in onChanged of the observer, but the problem is the textView is not updating every increment and I'm only getting the last count which 9.

Here is my code.

MainActivity

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    binding.setNumber(model);
    binding.setLifecycleOwner(this);

    model = ViewModelProviders.of(this).get(MainActivityViewModel.class);

    textView = findViewById(R.id.textView);

    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {
                model.incrementNumber();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });

    model.getNumber().observe(this, new Observer<String>() {
        @Override
        public void onChanged(final String s) {
            Log.d(TAG, s);
            textView.setText(s);
        }
    });

}

And here is the viewModel

public class MainActivityViewModel extends AndroidViewModel {

MutableLiveData<String> number;

public MainActivityViewModel(@NonNull Application application) {
    super(application);
    number = new MutableLiveData<String>("0");
}

public MutableLiveData<String> getNumber(){
    return number;
}

public void incrementNumber() throws InterruptedException {
    for(int x = 0; x < 10 ; x++){

        Thread.sleep(500);

        number.setValue(String.valueOf(x));
    }

}

}

Nms03
  • 15
  • 5
  • why do you need `Thread.sleep(500);`? – Raghunandan Dec 09 '19 at 17:43
  • I'm trying to test if data will be captured from the observer on every increment. Well, it turns out that it is being captured...but I'm stuck on updating the UI – Nms03 Dec 09 '19 at 17:46
  • 1
    it is working as intended. Also do not use thread.sleep. see https://github.com/googlecodelabs/android-lifecycles/blob/master/app/src/main/java/com/example/android/lifecycles/step3_solution/LiveDataTimerViewModel.java a timer and when you observer the same and update textview it gets updated. You can play with the sample – Raghunandan Dec 09 '19 at 18:12
  • Thanks for the added input. – Nms03 Dec 09 '19 at 18:18

1 Answers1

1

Try using a main thread Handler.postDelayed that will increment the number and post again to the Handler instead of Thread.sleep.

JacquesBauer
  • 226
  • 2
  • 7
  • Thanks, but it is still only updating on the final value. – Nms03 Dec 09 '19 at 18:00
  • Thanks, I sorted it out and add delay on the Handler..Not sure why Thread.sleep is not working for this scenario...anyway here is what I did in the incrementNumber(). int delay = 1000; for(int x = 0; x < 10 ; x++){ Handler handler = new Handler(); final int finalX = x; handler.postDelayed(new Runnable() { @Override public void run() { number.setValue(String.valueOf(finalX)); } }, delay); delay += 1000; } – Nms03 Dec 09 '19 at 18:13