I am new to android development i have a calulator page wherein i need to do small calculation on options selected from two spinner. I have used switch case to get results.
All UI is coming as expected but when i calculate using button results are not as expected and random.
can anyone point what mistake i am making??
activity
public class Calculatorrecon extends Activity implements OnItemSelectedListener {
double size, speed, result1, result2;
Button but1;
TextView result;
Spinner shoesize, shoespeed;
String[] size = { "Select Size", "100 cm", "80 cm",
"70 cm", "60 cm", "50 cm", "40 cm", "30 cm", };
String[] speed = { "Select Speed", "40", "50", "60", "70", "80", "90", "100", };
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.calculator);
result = (TextView) findViewById(R.id.speed);
but1 = (Button) findViewById(R.id.but);
ArrayAdapter<String> speedadapter = new ArrayAdapter<String>(
Calculatorrecon.this, android.R.layout.simple_spinner_item,
speed);
speedadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
shoespeed = (Spinner) findViewById(R.id.speed48);
shoespeed.setAdapter(speedadapter);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
Calculatorrecon.this, android.R.layout.simple_spinner_item,
size);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
shoesize = (Spinner) findViewById(R.id.size);
shoesize.setAdapter(adapter);
shoesize.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
int positition = shoesize.getSelectedItemPosition();
switch (positition) {
case 0:
break;
case 1:
size = 100;
break;
case 2:
size = 80;
break;
case 3:
size = 70;
break;
case 4:
size = 60;
break;
case 5:
size = 50;
break;
case 6:
size = 40;
break;
case 7:
size = 30;
break;
}
int speedz = shoespeed.getSelectedItemPosition();
switch (speedz) {
case 0:
break;
case 1:
speed = 4;
break;
case 2:
speed = 5;
break;
case 3:
speed = 6;
break;
case 4:
speed = 7;
break;
case 5:
speed = 8;
break;
case 6:
speed = 9;
break;
case 7:
speed = 10;
break;
}
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
calculate();
result.setText(String.format("%.1f", result2) + "");
}
});
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
private void calculate() {
result2 = 0;
double x = size * 8.4563764;
double y = 167.2 * speed;
result2 = y / x;
}