If-else working, switch not [duplicate]

You need to break; after each statement in a case, otherwise execution flows down (all cases below the one you want will also get called), so you’ll always get the last case.

switch(position) {
case 0:
    textView.setText(R.string.zero); 
    break; 
case 1:
    textView.setText(R.string.one);
    break; 
case 2:
    textView.setText(R.string.two);   
    break;  
case 3:
    textView.setText(R.string.three);
    break; 
case 4:
    textView.setText(R.string.four); 
    break; 
}

Here’s the official tutorial explaining when to and when not to use break;.

Leave a Comment