Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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.

Why is the switch statement not executing the correct case blocks?

+1
−0

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?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

1 answer

+5
−0

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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Response for future visitor!! You can edit if you want!! (1 comment)

Sign up to answer this question »