Switch where the case is the value of a cycle for

0

In my application I have implemented a button where pressing it shows a dialog with options to choose. These options are dynamic, that is to say, they are never going to be the same amount of options, because this makes it difficult for me to detect when one is chosen.

What I thought to solve the situation was the switch assign the amount of case necessary using the value of a cycle for , doing so gives me an error, when I put the pointer over case gives me this error Case statement outside switch .

Image showing the dialog

Used code:

 btn_show.setOnClickListener(new View.OnClickListener() { 

      @Override 

      public void onClick(View v) { 

           final String[] item =  text.split("\[")[1].split("\]")[0].split(","); 



          AlertDialog.Builder di = new AlertDialog.Builder(MainActivity.this); 

          di.setItems(item, new DialogInterface.OnClickListener() { 

              @Override 

              public void onClick(DialogInterface dialog, int which) { 

                  switch (which){ 

                     for (int i = 0; i < item.length; i++) { 

                          case i: { 

                              Toast.makeText(MainActivity.this, "Select " + item[i], Toast.LENGTH_SHORT).show(); 

                              break; 

                          } 



                      } 

                  } 

              } 

          }); 

          di.show(); 



      } 

  }); 

My question is: how could I solve this error or know if there is a better way to do this than I need ..

    
asked by cjamdeveloper 05.02.2018 в 00:18
source

1 answer

3

The problem is that the case is within the for , not the switch . What you should do is make the for and inside use just a if :

for (int i = 0; i < item.length; i++) { 
   if (which == i) {
       Toast.makeText(MainActivity.this, "Select " + item[i], Toast.LENGTH_SHORT).show(); 
       break; 
   }
}
    
answered by 05.02.2018 / 01:15
source