Created
December 8, 2017 16:18
-
-
Save nazmulidris/33729832c19cfa063a1ee2959cd15f28 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MainActivity extends AppCompatActivity { | |
| private TextView dataTextView; | |
| private TextView counterTextView; | |
| private StateViewModel stateViewModel; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| dataTextView = findViewById(R.id.data_textview); | |
| counterTextView = findViewById(R.id.counter_textview); | |
| setupModelView(); | |
| attachObservers(); | |
| } | |
| // Deal with loading state from ViewModel | |
| private void setupModelView() { | |
| stateViewModel = ViewModelProviders.of(this).get(StateViewModel.class); | |
| dataTextView.setText(String.format("Data: %s", stateViewModel.getData())); | |
| } | |
| private void attachObservers() { | |
| stateViewModel | |
| .getCounter() | |
| .observe( | |
| this, | |
| count -> { | |
| counterTextView.setText( | |
| String.format("Count: %s", Long.toString(count))); | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment