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

Dashboard
Notifications
Mark all as read
Q&A

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?

Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

1 answer

+4
−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.

Why does this post require moderator attention?
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 »