/
MainActivity.java
67 lines (42 loc) · 1.99 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
public class MainActivity extends Activity {
//RecyclerView declarations
private RecyclerView mRecyclerView;
private MainRecyclerAdapter mMainRecyclerAdapter;
//private final ArrayList<String> mProjectList = new ArrayList<Project>();
//Room Database declarations
ProjectDatabase mProjectDatabase;
//Create Singleton
Singleton singleton = Singleton.getInstance();
private void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity_layout);
//Room Database initialising & building
mProjectDatabase = Room.databaseBuilder(getApplicationContext(), ProjectDatabase.class, "PROJECT_DATABASE").build();
//Custom method for loading all Room Database projects using LiveData
loadAllProjects();
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
// Create an RecyclerView adapter and supply the data to be displayed.
mAdapter = new MainRecyclerAdapter(this, singleton.getProjectList());
// Connect the adapter with the recycler view.
mRecyclerView.setAdapter(mAdapter);
// Give the recycler view a default layout manager.
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//TODO:Add FAB for project add
//TODO: Add Toolbar and Options Menu
}
//Custom method for loading all Room Database projects using LiveData
private void loadAllProjects() {
mProjectDatabase.daoAcces().loadAllProjects().observe(this, new Observer<List<Project>>() {
@Override
public void onChanged(@Nullable List<Project> projectList) {
singleton.setProjectList(projectList);
mAdapter.mProjectList = singleton.getProjectList(); //why is this needed?
mAdapter.notifyDatasetChanged();
//Comment: maybe we should take the 2 lines above out of this method inyo onCreate? Because we are doing 2 things here. but notifOnDataSetChanged would work then?
}
});
}
}