Updating UI using ViewModel and DataBinding

后端 未结 3 547
清酒与你
清酒与你 2021-01-31 20:09

I am trying learn View-model android ,in my first phase of learning i am trying to update UI (Textview) by using view-model and data-binding. In View model i am having aynctask

3条回答
  •  梦谈多话
    2021-01-31 20:43

    If you want binding layout to work then you have to set your view in binding way. Also set data in binding class.

    public class MainActivity extends AppCompatActivity implements LifecycleOwner {
        ActivityMainBinding binding;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
            ...
            ViewModelData model = ViewModelProviders.of(this).get(ViewModelData.class);
            ...
            binding.setData(model.getUsers());
        }
    }
    

提交回复
热议问题