Switch语句在嵌套类型中失败

时间:2015-10-20 12:17:39

标签: java

我有以下开关:

我的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;
        }

但每次我的选择是:部门IS?收集NOCASE 和选择args来自外部开关的情况1。

所以这个:

 case 1:
                selection = Employee.COL_DEPARTMENT + " IS ? COLLATE NOCASE";
                selectionArgs = new String[]{valueReceived};
                break;

正在执行而不是:

case 2:
                        selection = Employee.COL_COUNTRY + " IS ? COLLATE NOCASE";
                        selectionArgs = new String[]{valueReceived};

但是,如果我评论外部开关的情况1和情况2.我得到了所需的结果。

我在这里缺少什么?

3 个答案:

答案 0 :(得分:3)

您忘记为顶级break添加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; //¯\_(ツ)_/¯

如果没有break;,流程就会继续并进入下一个case块,直到它达到break语句(或直到它超出switch)。

此外,为嵌套break添加case 2并不是一件好事(以防万一您将来添加更多case并且您可能会遇到同样的问题就像这一个,顺便称之为 fall-through )。

答案 1 :(得分:3)

你错过了休息时间:

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;
}

答案 2 :(得分:1)

您不要从外部开关盒中断。在内部开关的右括号处添加break

case 0:
    switch (childPosition) {
    //
    } break;