Reputation: 2535
I have the following switch:
My groupPosition is 0 and childPosition is 1:
switch (groupPosition) {
case 0:
switch (childPosition) {
case 0:
if (NetworkManager.isNetworkAvailable(this)) {
new UserAwayTask().execute();
} else
Toast.makeText(this, "Network not available", Toast.LENGTH_LONG).show();
break;
case 1:
selection = null;
selectionArgs = null;
break;
case 2:
selection = Employee.COL_COUNTRY + " IS ? COLLATE NOCASE";
selectionArgs = new String[]{valueReceived};
}
case 1:
selection = Employee.COL_DEPARTMENT + " IS ? COLLATE NOCASE";
selectionArgs = new String[]{valueReceived};
break;
case 2:
empIDList = GetAllTeamLeaders.teamLeaders(this, TeamLeader.COL_TEAMMEMBERID, TeamLeader.COL_TEAMLEADERNAME + " IS ? ", new String[]{valueReceived});
selection = Employee.COL_EMPID + " IN (" + TextUtils.join(",", Collections.nCopies(empIDList.size(), "?")) + ")";
selectionArgs = empIDList.toArray(new String[empIDList.size()]);
break;
}
But each time my selection is: department IS ? COLLATE NOCASE and selection args is from case 1 from the outer switch.
So this:
case 1:
selection = Employee.COL_DEPARTMENT + " IS ? COLLATE NOCASE";
selectionArgs = new String[]{valueReceived};
break;
is getting executed instead of :
case 2:
selection = Employee.COL_COUNTRY + " IS ? COLLATE NOCASE";
selectionArgs = new String[]{valueReceived};
However if I comment the outer switch's case 1 and case 2. I get the desired result.
What am I missing here?
Upvotes: 0
Views: 72
Reputation: 62864
You forgot to add a break
for the top-level case 0
:
case 0:
switch (childPosition) {
case 0:
if (NetworkManager.isNetworkAvailable(this)) {
new UserAwayTask().execute();
} else
Toast.makeText(this, "Network not available", Toast.LENGTH_LONG).show();
break;
case 1:
selection = null;
selectionArgs = null;
break;
case 2:
selection = Employee.COL_COUNTRY + " IS ? COLLATE NOCASE";
selectionArgs = new String[]{valueReceived};
break;
}
break; //¯\_(ツ)_/¯
Without the break;
, the flow just proceeds and enters the next case
block until it reaches a break
statement (or until it goes out of the switch
).
Also, it's not bad idea to add a break
for the nested case 2
(just in case you add more case
s in the future and you might end up with the same problem as this one, which by the way is called fall-through).
Upvotes: 3
Reputation: 455
Your are missing a break:
switch (groupPosition) {
case 0:
switch (childPosition) {
...
}
break; // <-- here
case 1:
selection = Employee.COL_DEPARTMENT + " IS ? COLLATE NOCASE";
selectionArgs = new String[]{valueReceived};
break;
case 2:
empIDList = GetAllTeamLeaders.teamLeaders(this, TeamLeader.COL_TEAMMEMBERID, TeamLeader.COL_TEAMLEADERNAME + " IS ? ", new String[]{valueReceived});
selection = Employee.COL_EMPID + " IN (" + TextUtils.join(",", Collections.nCopies(empIDList.size(), "?")) + ")";
selectionArgs = empIDList.toArray(new String[empIDList.size()]);
break;
}
Upvotes: 3
Reputation: 21975
You do not break from your outer switch case 0. Add a break
at the closing bracket of your inner switch.
case 0:
switch (childPosition) {
//
} break;
Upvotes: 1