Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
The problem is the fallthrough behaviour of case statements. Basically, once a case's condition is met, all the others after that are also executed. Example: int x = 2; switch (x) { case 1: ...
Answer
#2: Post edited
- The problem is the [fallthrough behaviour of `case` statements](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html#:~:text=Another%20point%20of%20interest%20is%20the%20break%20statement). Basically, once a `case`'s condition is met, all the others after that are also executed. Example:
- ```java
- int x = 2;
- switch (x) {
- case 1:
- System.out.println("one");
- case 2:
- System.out.println("two");
- case 3:
- System.out.println("three");
- default:
- System.out.println("default");
- }
- ```
- The output of this code is:
- ```none
- two
- three
- default
- ```
Note that, once it enters in one of the `case` conditions, all the others after it are executed as well. If you don't want that to happen, you need to add a `break` statement if each `case`:- ```java
- int x = 2;
- switch (x) {
- case 1:
- System.out.println("one");
- break;
- case 2:
- System.out.println("two");
- break;
- case 3:
- System.out.println("three");
- break;
- default:
- System.out.println("default");
- }
- ```
- Now it prints only `two`.
- ---
- That's the problem in your code. When `i` is `0`, it enters the first `case` and also executes all the others. Adding a `break` statement should be enough.
- But I also see a lot of repetition in your code. Note that inside each `case`, most of the code is basically the same, and only one or another parameter changes. That said, perhaps a good refactor would be something like this:
- ```java
- String order = null;
- switch (i) {
- case 0:
- order = CallLog.Calls.DATE + " DESC ";
- break;
- case 1:
- order = CallLog.Calls.DATE + " ASC ";
- break;
- case 2:
- order = CallLog.Calls.CACHED_NAME + " ASC ";
- break;
- default:
- order = CallLog.Calls.DATE + " DESC ";
- }
- if (order != null) {
- new Handler().postDelayed(() -> {
- contactViewAdapter = new ContactViewAdapter(getActivity(), getActivity(), new ReadContacts().CallLog(getActivity(), order), "dialer");
- recyclerViewCallLog.swapAdapter(contactViewAdapter, true);
- recyclerViewCallLog.setAdapter(contactViewAdapter);
- recyclerViewCallLog.invalidate();
- recyclerViewCallLog.getAdapter().notifyDataSetChanged();
- }, 1000);
- }
- ```
- I couldn't test because I don't have all those classes here, but that's the basic idea: in the `switch` statement, you set all the data that changes, and after that you use those data and do whatever you need.
- The problem is the [fallthrough behaviour of `case` statements](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html#:~:text=Another%20point%20of%20interest%20is%20the%20break%20statement). Basically, once a `case`'s condition is met, all the others after that are also executed. Example:
- ```java
- int x = 2;
- switch (x) {
- case 1:
- System.out.println("one");
- case 2:
- System.out.println("two");
- case 3:
- System.out.println("three");
- default:
- System.out.println("default");
- }
- ```
- The output of this code is:
- ```none
- two
- three
- default
- ```
- Note that, once it enters in one of the `case` conditions, all the others after it are executed as well. If you don't want that to happen, you need to add a `break` statement in each `case`:
- ```java
- int x = 2;
- switch (x) {
- case 1:
- System.out.println("one");
- break;
- case 2:
- System.out.println("two");
- break;
- case 3:
- System.out.println("three");
- break;
- default:
- System.out.println("default");
- }
- ```
- Now it prints only `two`.
- ---
- That's the problem in your code. When `i` is `0`, it enters the first `case` and also executes all the others. Adding a `break` statement should be enough.
- But I also see a lot of repetition in your code. Note that inside each `case`, most of the code is basically the same, and only one or another parameter changes. That said, perhaps a good refactor would be something like this:
- ```java
- String order = null;
- switch (i) {
- case 0:
- order = CallLog.Calls.DATE + " DESC ";
- break;
- case 1:
- order = CallLog.Calls.DATE + " ASC ";
- break;
- case 2:
- order = CallLog.Calls.CACHED_NAME + " ASC ";
- break;
- default:
- order = CallLog.Calls.DATE + " DESC ";
- }
- if (order != null) {
- new Handler().postDelayed(() -> {
- contactViewAdapter = new ContactViewAdapter(getActivity(), getActivity(), new ReadContacts().CallLog(getActivity(), order), "dialer");
- recyclerViewCallLog.swapAdapter(contactViewAdapter, true);
- recyclerViewCallLog.setAdapter(contactViewAdapter);
- recyclerViewCallLog.invalidate();
- recyclerViewCallLog.getAdapter().notifyDataSetChanged();
- }, 1000);
- }
- ```
- I couldn't test because I don't have all those classes here, but that's the basic idea: in the `switch` statement, you set all the data that changes, and after that you use those data and do whatever you need.
#1: Initial revision
The problem is the [fallthrough behaviour of `case` statements](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html#:~:text=Another%20point%20of%20interest%20is%20the%20break%20statement). Basically, once a `case`'s condition is met, all the others after that are also executed. Example: ```java int x = 2; switch (x) { case 1: System.out.println("one"); case 2: System.out.println("two"); case 3: System.out.println("three"); default: System.out.println("default"); } ``` The output of this code is: ```none two three default ``` Note that, once it enters in one of the `case` conditions, all the others after it are executed as well. If you don't want that to happen, you need to add a `break` statement if each `case`: ```java int x = 2; switch (x) { case 1: System.out.println("one"); break; case 2: System.out.println("two"); break; case 3: System.out.println("three"); break; default: System.out.println("default"); } ``` Now it prints only `two`. --- That's the problem in your code. When `i` is `0`, it enters the first `case` and also executes all the others. Adding a `break` statement should be enough. But I also see a lot of repetition in your code. Note that inside each `case`, most of the code is basically the same, and only one or another parameter changes. That said, perhaps a good refactor would be something like this: ```java String order = null; switch (i) { case 0: order = CallLog.Calls.DATE + " DESC "; break; case 1: order = CallLog.Calls.DATE + " ASC "; break; case 2: order = CallLog.Calls.CACHED_NAME + " ASC "; break; default: order = CallLog.Calls.DATE + " DESC "; } if (order != null) { new Handler().postDelayed(() -> { contactViewAdapter = new ContactViewAdapter(getActivity(), getActivity(), new ReadContacts().CallLog(getActivity(), order), "dialer"); recyclerViewCallLog.swapAdapter(contactViewAdapter, true); recyclerViewCallLog.setAdapter(contactViewAdapter); recyclerViewCallLog.invalidate(); recyclerViewCallLog.getAdapter().notifyDataSetChanged(); }, 1000); } ``` I couldn't test because I don't have all those classes here, but that's the basic idea: in the `switch` statement, you set all the data that changes, and after that you use those data and do whatever you need.