[英]Switch statement fails in nested type
I have the following switch:我有以下开关:
My groupPosition is 0 and childPosition is 1:我的 groupPosition 为 0,childPosition 为 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.
COLLATE NOCASE 和 selection args 来自外部开关的案例 1。
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.但是,如果我评论外部开关的案例 1 和案例 2。我得到了想要的结果。
What am I missing here?我在这里缺少什么?
You forgot to add a break
for the top-level case 0
:您忘记为顶级
case 0
添加break
:
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;
没有
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
). ,流程只是继续并进入下一个
case
块,直到它到达break
语句(或直到它离开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 ).此外,为嵌套的
case 2
添加一个break
也不错(以防万一您将来添加更多case
并且您最终可能遇到与此相同的问题,顺便说一下,它称为fall-through ) .
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;
}
You do not break from your outer switch case 0. Add a break
at the closing bracket of your inner switch.您不会从外部开关盒 0
break
在内部开关的右括号处添加一个break
。
case 0:
switch (childPosition) {
//
} break;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.