2015-10-20 94 views
0

失敗,我有以下開關:switch語句中嵌套類型

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

但每次我的選擇是:部門是什麼? COLLATE NOCASE ,選擇參數來自外部開關的情況1。

所以這個:如果我評論外側開關的情況下,1和2的情況下

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; 

是越來越執行代替。

我在這裏錯過了什麼?

+2

你缺少break語句的情況下0: – arados

+1

你需要在'情況下,'之開關語句後分號和'break' 0'或者它會在完成時直接進入'情況1'。 – lurker

回答

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 S和你可能最終得到了同樣的問題,因爲這一個,它的方式被稱爲跌破)。

+0

我錯過了!恐怖!感謝隊友 – User3

+0

通過,我不能用言語表達我多麼感激你:) – User3

+0

不客氣。 :-) –

1

您不會從您的外部開關外殼0中斷。在內部開關的閉合支架上添加一個break

case 0: 
    switch (childPosition) { 
    // 
    } break; 
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; 
}