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.
Comments on Why is the switch statement not executing the correct case blocks?
Parent
Why is the switch statement not executing the correct case blocks?
In the code below, the value of order
variable should change according to i
's value:
String order;
switch (i){
case 0:
order = CallLog.Calls.DATE + " DESC ";
contactViewAdapter = new ContactViewAdapter(getActivity(),getActivity(),new ReadContacts().CallLog(getActivity(), order),"dialer");
recyclerViewCallLog.swapAdapter(contactViewAdapter,true);
recyclerViewCallLog.setAdapter(contactViewAdapter);
recyclerViewCallLog.invalidate();
recyclerViewCallLog.getAdapter().notifyDataSetChanged();
case 1:
order = CallLog.Calls.DATE+" ASC ";
contactViewAdapter = new ContactViewAdapter(getActivity(),getActivity(),new ReadContacts().CallLog(getActivity(), order),"dialer");
recyclerViewCallLog.swapAdapter(contactViewAdapter,true);
recyclerViewCallLog.setAdapter(contactViewAdapter);
recyclerViewCallLog.invalidate();
recyclerViewCallLog.getAdapter().notifyDataSetChanged();
case 2:
order = CallLog.Calls.CACHED_NAME+" ASC ";
contactViewAdapter = new ContactViewAdapter(getActivity(),getActivity(),new ReadContacts().CallLog(getActivity(), order),"dialer");
recyclerViewCallLog.swapAdapter(contactViewAdapter,true);
recyclerViewCallLog.setAdapter(contactViewAdapter);
recyclerViewCallLog.invalidate();
recyclerViewCallLog.getAdapter().notifyDataSetChanged();
default:
//contactViewAdapter = adapter(CallLog.Calls.DATE+" DESC ");
//recyclerViewCallLog.invalidate();
//recyclerViewCallLog.setAdapter(contactViewAdapter);
}
When i
is 0
, the order should be DESC
, but it's actually ASC
. I've updated the code to this:
final String[] order = new String[1];
switch (i){
case 0:
new Handler().postDelayed(() -> {
order[0] = CallLog.Calls.DATE + " DESC ";
contactViewAdapter = new ContactViewAdapter(getActivity(), getActivity(), new ReadContacts().CallLog(getActivity(), order[0]), "dialer");
recyclerViewCallLog.swapAdapter(contactViewAdapter, true);
recyclerViewCallLog.setAdapter(contactViewAdapter);
recyclerViewCallLog.invalidate();
recyclerViewCallLog.getAdapter().notifyDataSetChanged();
},1000);
case 1:
new Handler().postDelayed(() -> {
order[0] = CallLog.Calls.DATE+" ASC ";
contactViewAdapter = new ContactViewAdapter(getActivity(),getActivity(),new ReadContacts().CallLog(getActivity(), order[0]),"dialer");
recyclerViewCallLog.swapAdapter(contactViewAdapter,true);
recyclerViewCallLog.setAdapter(contactViewAdapter);
recyclerViewCallLog.invalidate();
recyclerViewCallLog.getAdapter().notifyDataSetChanged();
},1000);
case 2:
new Handler().postDelayed(() -> {
order[0] = CallLog.Calls.CACHED_NAME+" ASC ";
contactViewAdapter = new ContactViewAdapter(getActivity(),getActivity(),new ReadContacts().CallLog(getActivity(), order[0]),"dialer");
recyclerViewCallLog.swapAdapter(contactViewAdapter,true);
recyclerViewCallLog.setAdapter(contactViewAdapter);
recyclerViewCallLog.invalidate();
recyclerViewCallLog.getAdapter().notifyDataSetChanged();
},1000);
default:
new Handler().postDelayed(() -> {
order[0] = CallLog.Calls.DATE + " DESC ";
contactViewAdapter = new ContactViewAdapter(getActivity(), getActivity(), new ReadContacts().CallLog(getActivity(), order[0]), "dialer");
recyclerViewCallLog.swapAdapter(contactViewAdapter, true);
recyclerViewCallLog.setAdapter(contactViewAdapter);
recyclerViewCallLog.invalidate();
recyclerViewCallLog.getAdapter().notifyDataSetChanged();
},1000);
}
But now it doesn't work when i
is 1
or 2
: it should be ASC
but in those cases it ends up being DESC
.
How can I fix this?
Post
The following users marked this post as Works for me:
User | Comment | Date |
---|---|---|
Anonymous | (no comment) | Oct 8, 2021 at 04:33 |
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:
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:
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
:
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:
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.
0 comment threads