From 5a6e968efe2985d35b05b3143acd20e0ef2968ce Mon Sep 17 00:00:00 2001 From: minhtrannhat Date: Sun, 5 Nov 2023 14:40:49 -0500 Subject: [PATCH] Feat(main): Toggle Display Mode --- .../Views/MainActivity.java | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/example/coen390_assignment2/Views/MainActivity.java b/app/src/main/java/com/example/coen390_assignment2/Views/MainActivity.java index 94a7fa1..b7d67e3 100644 --- a/app/src/main/java/com/example/coen390_assignment2/Views/MainActivity.java +++ b/app/src/main/java/com/example/coen390_assignment2/Views/MainActivity.java @@ -48,15 +48,15 @@ public class MainActivity extends AppCompatActivity { studentProfileDBHelper = new StudentProfileDBHelper(getApplicationContext()); initAddProfileActionButton(showDialogButton); + + // surname when true, ID when false + profileNameDisplayMode = true; } @Override protected void onStart() { super.onStart(); - // surname when true, ID when false - profileNameDisplayMode = true; - List studentProfiles = studentProfileDBHelper.getAllStudentProfile(getApplicationContext()); // by default sort by surname @@ -86,6 +86,26 @@ public class MainActivity extends AppCompatActivity { return true; } + // toggle Display mode + @Override + public boolean onOptionsItemSelected(MenuItem item) { + int id = item.getItemId(); + + if (id == R.id.toggle_profiles_display_mode && toggle.getTitle() == "By ID") { + profileNameDisplayMode = false; + onStart(); + + return true; + } else if (id == R.id.toggle_profiles_display_mode && toggle.getTitle() == "By Surname"){ + profileNameDisplayMode = true; + + onStart(); + + return true; + } + + return super.onOptionsItemSelected(item); + } protected String[] showStudentProfileList(List studentProfiles, boolean profileNameDisplayMode){ // Create a new list for sorted profiles List sortedProfiles = new ArrayList<>(studentProfiles);