Skip to content

Instantly share code, notes, and snippets.

@PierceZ
Created October 2, 2017 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PierceZ/8d02740af2ae1c6401a06cb8269c99f2 to your computer and use it in GitHub Desktop.
Save PierceZ/8d02740af2ae1c6401a06cb8269c99f2 to your computer and use it in GitHub Desktop.
public class MainActivity extends AppCompatActivity {
private ZooListViewModel mViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.activity_main_recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
ZooAdapter adapter = new ZooAdapter();
adapter.setOnClickListener((view, position) -> {
Intent zooIntent = new Intent(MainActivity.this, ZooActivity.class);
zooIntent.putExtra(ZooActivity.EXTRA_ZOO_ID, adapter.getItemId(position));
startActivity(zooIntent);
});
recyclerView.setAdapter(adapter);
mViewModel = ViewModelProviders.of(this).get(ZooListViewModel.class);
mViewModel.getZoos().observe(this, (adapter::update));
findViewById(R.id.activity_main_fab).setOnClickListener(v -> {
DialogFragment zooFragment = ZooFragment.newInstance();
zooFragment.show(getSupportFragmentManager(), ZooFragment.class.getName());
});
}
@Override
protected void onResume() {
super.onResume();
mViewModel.refreshZoos();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment