I have a students_table and there are stored students of different levels. I want to display students by one level and hide other levels. I select student to show like this:
if (id == R.id.beginners) {
stLvl = 0;
}else if (id == R.id.intermediate) {
stLvl = 1;
}else if (id == R.id.advanced) {
stLvl = 2;
}else if (id == R.id.high_level) {
stLvl = 3;
}
showStud();
And here it is showStud ();
public void showStud() {
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
final StudentAdapter adapter = new StudentAdapter();
recyclerView.setAdapter(adapter);
setStLvl(stLvl);
if (stLvl == 0) {
studentViewModel = ViewModelProviders.of(this).get(StudentViewModel.class);
studentViewModel.getAllStudents().observe(this, new Observer<List<Student>>() {
@Override
public void onChanged(@Nullable List<Student> students) {
// update RecyclerView
adapter.submitList(students);
}
});
}else {
studentViewModel = ViewModelProviders.of(this).get(StudentViewModel.class);
studentViewModel.getStudentsByLevel().observe(this, new Observer<List<Student>>() {
@Override
public void onChanged(@Nullable List<Student> students) {
// update RecyclerView
adapter.submitList(students);
}
});
}
}
First time when the code run it works perfect, no matter the value of stLvl, but when I change it's value is not displaying what I want, or nothing at all. I think the problem is at this line:
studentViewModel = ViewModelProviders.of(this).get(StudentViewModel.class);
First time it runs, it is working ok, going to StudentViewModel.class doing what is supposed to do, but second time just jumps to next line of code, without going to StudentViewModel.class. What am I doing wrong? Thank you in advance!
First of all, reading this Guide to app architecture will help you get the general idea of how these architectural components should work together. The rule of thumb is,
This also means that each component should not depend on the components above it. For example, the repository should not depend on neither ViewModels nor Activities. Your code can be refactored in this way:
StudentRepository:
In the above example, the repository looks like it doesn't do much, and that is normal because there is only one layer below it, Room. In real practice you can have other data sources including network clients and cache. The repository's job is to abstract all data source logics.
ViewModel:
I am not a fan of
LiveData
, but here's what I would do. Keep all of your logic in ViewModel and make the view layer as simple as possible.Lastly, Activity:
Above code will show all students because we set the default value to 0 in the viewmodel. Call
studentViewModel.setValue(/*any integer*/)
to switch the list to any level.