Reputation: 10199
What I have is an ImageView that the user will swipe and on a swipe a variable called plane
is changed and the switch case is for the variable plane
that I am having the issue with. This is my code :
(the below switch case is triggered on a swipe of the image view)
switch(plane){
...
case 77:
Log.d("NICK","Case 77 Left Swipe");
plane = 78;
Picasso.with(context).load(R.drawable.plane77).into(image2);
plane_name.setText(plane_names[plane-1]);
break;
case 78:
//********************************************************************
Log.d("NICK","Case 78 Left Swipe");
plane = 79;
Picasso.with(context).load(R.drawable.plane78).into(image2);
plane_name.setText(plane_names[plane-1]);
//*********************************************************************
case 79:
Log.d("NICK","Case 79 Left Swipe");
plane = 80;
Picasso.with(context).load(R.drawable.plane79).into(image2);
plane_name.setText(plane_names[plane-1]);
case 80:
Log.d("NICK","Case 80 Left Swipe ");
plane = 81;
Picasso.with(context).load(R.drawable.plane80).into(image2);
plane_name.setText(plane_names[plane-1]);
case 81:
Log.d("NICK","Case 81 Left Swipe ");
plane = 82;
Picasso.with(context).load(R.drawable.plane81).into(image2);
plane_name.setText(plane_names[plane-1]);
case 82:
Log.d("NICK","Case 82 Left Swipe ");
plane = 83;
Picasso.with(context).load(R.drawable.plane82).into(image2);
plane_name.setText(plane_names[plane-1]);
case 83:
Log.d("NICK","Case 83 Left Swipe ");
plane = 84;
Picasso.with(context).load(R.drawable.plane83).into(image2);
plane_name.setText(plane_names[plane-1]);
case 84:
Log.d("NICK","Case 84 Left Swipe ");
plane = 85;
Picasso.with(context).load(R.drawable.plane84).into(image2);
plane_name.setText(plane_names[plane-1]);
....
}
and the issue is that for whatever reason once case 78 is reached, cases 78,79,80,81 are triggered at the same time as indicated by my print statements below. The expected result is for 1 case to be triggered at a time in order to change the value of plane
appropriately. There are cases up to 77 that are similar, and they increment plane
to always be +1. These previous cases behave as expected.
Logcat output when running:
07-08 20:21:24.010: DEBUG/NICK(9715): Case 72 Left Swipe
07-08 20:21:25.572: DEBUG/NICK(9715): Case 73 Left Swipe
07-08 20:21:27.183: DEBUG/NICK(9715): Case 74 Left Swipe
07-08 20:21:34.491: DEBUG/NICK(9715): Case 75 Left Swipe
07-08 20:21:36.944: DEBUG/NICK(9715): Case 76 Left Swipe
07-08 20:21:39.537: DEBUG/NICK(9715): Case 77 Left Swipe
//below cases 78,79,80,81 are triggered at the same time
07-08 20:21:42.600: DEBUG/NICK(9715): Case 78 Left Swipe
07-08 20:21:42.600: DEBUG/NICK(9715): Case 79 Left Swipe
07-08 20:21:42.600: DEBUG/NICK(9715): Case 80 Left Swipe
07-08 20:21:42.600: DEBUG/NICK(9715): Case 81 Left Swipe
07-08 20:21:42.610: DEBUG/NICK(9715): Case 82 Left Swipe
07-08 20:21:42.610: DEBUG/NICK(9715): Case 83 Left Swipe
07-08 20:21:42.610: DEBUG/NICK(9715): Case 84 Left Swipe
Any suggestions for what could be causing this issue? I suspect it must be a logical error that I have overlooked but I cant find any mistakes in what I have.
Upvotes: 0
Views: 98
Reputation: 55340
As the other answers say, you're missing break;
However, I would highly recommend refactoring this code...
For example, have a Map<Integer, Integer>
mapping plane number to resource ids. Then load it at startup:
Map<Integer, Integer> mImages = new HashMap<Integer, Integer>();
mImages.put(77, R.drawable.plane_77);
mImages.put(78, R.drawable.plane_78);
mImages.put(79, R.drawable.plane_79);
...
Then you can replace this gigantic switch statement with something like:
plane -= 1;
Picasso.with(context).load(mImageMap.get(plane)).into(image2);
plane_name.setText(plane_names[plane]);
Upvotes: 4
Reputation: 74
It isnt an unusual behaviour. You forgot to write break after the code block after case 77.
Upvotes: 1
Reputation: 10223
You don't have a break;
for 79, 80 ... so on. So to fix this:
case 79:
Log.d("NICK","Case 79 Left Swipe");
plane = 80;
Picasso.with(context).load(R.drawable.plane79).into(image2);
plane_name.setText(plane_names[plane-1]);
break;
case 80:
Log.d("NICK","Case 80 Left Swipe ");
plane = 81;
Picasso.with(context).load(R.drawable.plane80).into(image2);
plane_name.setText(plane_names[plane-1]);
break;
You see, what the break
statement does is tells it to break out of the statements. If there's no break statement, it continues down the line and executes the code for 80, and then 81, and then 82, and so on and so forth.
Upvotes: 3
Reputation: 11
You have a break statement for 77, but not the others. Is that really what you want?
The C style switch/case statement has always seemed daft to me - 99.9% of cases (pun intended) you want break, why not make it the default?
Upvotes: 0