I am making an app for my final year project. It is a BMI calculator that tells the user their BMR too. I have a Gender spinner with two entries. What is meant to happen is that when male is selected the spinner should do a calculation and if the other spinner is selected the spinner should do an alternative calculation. This works however there is a bug. When the users presses next there is no result, the user has the press back select the gender again then press next then the result will show. I have no idea what is causing this problem. I have a goggle code link if there is any contributes willing. I will make sure I will reference you in my documentation.
google code site http://code.google.com/p/egoapp/
Code:
[FONT=Arial][FONT=Arial]package com.FYP.EGO;[/FONT]
[FONT=Arial]import java.util.ArrayList;[/FONT]
[FONT=Arial]import java.util.List;[/FONT]
[FONT=Arial]import android.app.Activity;[/FONT]
[FONT=Arial]import android.app.AlertDialog;[/FONT]
[FONT=Arial]import android.content.DialogInterface;[/FONT]
[FONT=Arial]import android.content.Intent;[/FONT]
[FONT=Arial]import android.os.Bundle;[/FONT]
[FONT=Arial]import android.view.View;[/FONT]
[FONT=Arial]import android.widget.AdapterView;[/FONT]
[FONT=Arial]import android.widget.ArrayAdapter;[/FONT]
[FONT=Arial]import android.widget.Button;[/FONT]
[FONT=Arial]import android.widget.Spinner;[/FONT]
[FONT=Arial]import android.widget.EditText;[/FONT]
[FONT=Arial]import android.widget.TextView;[/FONT]
[FONT=Arial]import android.widget.AdapterView.OnItemSelectedListener;[/FONT]
[FONT=Arial]public class WLactivity1 extends Activity {[/FONT]
[FONT=Arial]private Spinner spnGender;[/FONT]
[FONT=Arial]private Spinner spnFreq;[/FONT]
[FONT=Arial]private EditText txtwlHeight;[/FONT]
[FONT=Arial]private EditText txtwlWeight;[/FONT]
[FONT=Arial]private EditText txtwlHeight2;[/FONT]
[FONT=Arial]private EditText txtageinput01;[/FONT]
[FONT=Arial]private double wlHeightM ;[/FONT]
[FONT=Arial]private double wlHeightC ;[/FONT]
[FONT=Arial]private double wlHeight ;[/FONT]
[FONT=Arial]private double wlWeight ;[/FONT]
[FONT=Arial]private double wlHeight2;[/FONT]
[FONT=Arial]private int age;[/FONT]
[FONT=Arial]private Button btnWlnext;[/FONT]
[FONT=Arial]private static double bmiresult;[/FONT]
[FONT=Arial]public static String wlbmi;[/FONT]
[FONT=Arial]public static String bmiclass;[/FONT]
[FONT=Arial]public static String wlbmr;[/FONT]
[FONT=Arial]public static String s;[/FONT]
[FONT=Arial]private double BMR;[/FONT]
[FONT=Arial]public void onCreate(Bundle savedInstanceState) {[/FONT]
[FONT=Arial] super.onCreate(savedInstanceState);[/FONT]
[FONT=Arial] setContentView(R.layout.main2);[/FONT]
[FONT=Arial] {[/FONT]
[FONT=Arial] spnGender= (Spinner)findViewById(R.id.spnGender);[/FONT]
[FONT=Arial] spnFreq = (Spinner) findViewById(R.id.spnFreq);[/FONT]
[FONT=Arial] txtwlHeight = (EditText) findViewById(R.id.heightinput01);[/FONT]
[FONT=Arial] txtwlHeight2 = (EditText) findViewById(R.id.heightinput02);[/FONT]
[FONT=Arial] txtwlWeight = (EditText) findViewById(R.id.weightinput01);[/FONT]
[FONT=Arial] txtageinput01 = (EditText) findViewById(R.id.ageinput01);[/FONT]
[FONT=Arial] btnWlnext = (Button) findViewById(R.id.btnWLnext);[/FONT]
[FONT=Arial] //this disables the next button when the activity starts[/FONT]
[FONT=Arial] // btnWlnext.setEnabled(false);[/FONT]
[FONT=Arial] //Creating Keylistener the Next button only becomes active where there fields have valid vaules , if this is not implemented the program will crash [/FONT]
[FONT=Arial]}[/FONT]
[FONT=Arial] //adapting the spinner to use the list array[/FONT]
[FONT=Arial] ArrayAdapter<CharSequence> Gender= ArrayAdapter.createFromResource(this,R.array.genders, android.R.layout.simple_spinner_item);[/FONT]
[FONT=Arial] Gender.setDropDownViewResource[/FONT]
[FONT=Arial] (android.R.layout.simple_spinner_dropdown_item);[/FONT]
[FONT=Arial] spnGender.setAdapter(Gender);[/FONT]
[FONT=Arial] //adapting the spinner to use the list array[/FONT]
[FONT=Arial] ArrayAdapter<CharSequence> adapter1= ArrayAdapter.createFromResource(this,R.array.frequency, android.R.layout.simple_spinner_item);[/FONT]
[FONT=Arial] adapter1.setDropDownViewResource[/FONT]
[FONT=Arial] (android.R.layout.simple_spinner_dropdown_item);[/FONT]
[FONT=Arial] spnFreq.setAdapter(adapter1);[/FONT]
[FONT=Arial] // Next button that has multiple commands[/FONT]
[FONT=Arial] btnWlnext.setOnClickListener(new View.OnClickListener()[/FONT]
[FONT=Arial] {[/FONT]
[FONT=Arial] public void onClick(View View) [/FONT]
[FONT=Arial] {[/FONT]
[FONT=Arial] //boolean isError = false;[/FONT]
[FONT=Arial] if ((txtwlHeight.getText().toString()== " " )[/FONT]
[FONT=Arial] ||(txtwlHeight.getText().length()== 0 )[/FONT]
[FONT=Arial] ||(txtwlHeight2.getText().toString()== " ")[/FONT]
[FONT=Arial] ||(txtwlHeight2.getText().length()== 0 )[/FONT]
[FONT=Arial] ||(txtwlWeight.getText().toString()== " ")[/FONT]
[FONT=Arial] ||(txtwlWeight.getText().length()== 0)[/FONT]
[FONT=Arial] ||(txtageinput01.getText().toString()== " ")[/FONT]
[FONT=Arial] ||(txtageinput01.getText().length()== 0)[/FONT]
[FONT=Arial] )[/FONT]
[FONT=Arial] {[/FONT]
[FONT=Arial] showErrorAlert("Some Input are empty!", [/FONT]
[FONT=Arial] txtwlHeight.getId());[/FONT]
[FONT=Arial] }[/FONT]
[FONT=Arial] else [/FONT]
[FONT=Arial] {[/FONT]
[FONT=Arial] //converting numbers string[/FONT]
[FONT=Arial] wlHeightM=Double.parseDouble(txtwlHeight.getText().toString());[/FONT]
[FONT=Arial] wlHeightC=Double.parseDouble(txtwlHeight2.getText().toString());[/FONT]
[FONT=Arial] wlWeight=Double.parseDouble(txtwlWeight.getText().toString());[/FONT]
[FONT=Arial] age=(int) Double.parseDouble(txtageinput01.getText().toString());[/FONT]
[FONT=Arial] // convert to meters[/FONT]
[FONT=Arial] wlHeight = ((wlHeightM*100)+wlHeightC)/100;[/FONT]
[FONT=Arial] //covert to CM[/FONT]
[FONT=Arial] wlHeight2=((wlHeightM*100)+wlHeightC);[/FONT]
[FONT=Arial] bmiresult = wlWeight/(wlHeight*wlHeight);[/FONT]
[FONT=Arial] //round to 2 decimal places[/FONT]
[FONT=Arial] double newNum = Math.round(bmiresult*100.0)/100.0;[/FONT]
[FONT=Arial] wlbmi=(Double.toString(newNum));[/FONT]
[FONT=Arial] if (newNum < 18.5 ) [/FONT]
[FONT=Arial] {[/FONT]
[FONT=Arial] bmiclass = "Underweight";[/FONT]
[FONT=Arial] }[/FONT]
[FONT=Arial] if (newNum > 18.6)[/FONT]
[FONT=Arial] {[/FONT]
[FONT=Arial] bmiclass = "Normal weight";[/FONT]
[FONT=Arial] }[/FONT]
[FONT=Arial] if (newNum > 25)[/FONT]
[FONT=Arial] {[/FONT]
[FONT=Arial] bmiclass = "Overweight";[/FONT]
[FONT=Arial] }[/FONT]
[FONT=Arial] if (newNum > 30)[/FONT]
[FONT=Arial] {[/FONT]
[FONT=Arial] bmiclass = "Obesity (Class 1)";[/FONT]
[FONT=Arial] }[/FONT]
[FONT=Arial] if (newNum > 35)[/FONT]
[FONT=Arial] {[/FONT]
[FONT=Arial] bmiclass = "Obesity (Class 2)";[/FONT]
[FONT=Arial] }[/FONT]
[FONT=Arial] if (newNum > 40)[/FONT]
[FONT=Arial] {[/FONT]
[FONT=Arial] bmiclass = "Morbid Obesity";[/FONT]
[FONT=Arial] }[/FONT]
[FONT=Arial] //BMR formula [/FONT]
[FONT=Arial] //[/FONT]
[FONT=Arial] //setting a call back for spinner[/FONT]
[FONT=Arial] spnGender.setOnItemSelectedListener(new OnItemSelectedListener(){[/FONT]
[FONT=Arial] public void onNothingSelected(AdapterView<?> arg0){}[/FONT]
[FONT=Arial] @Override[/FONT]
[FONT=Arial] public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {[/FONT]
[FONT=Arial] // TODO Auto-generated method stub{ [/FONT]
[FONT=Arial] switch (position){[/FONT]
[FONT=Arial] case 0: [/FONT]
[FONT=Arial] BMR = 66 + ( 13.7 * wlWeight ) + ( 5 * wlHeight2 ) - ( 6.8 * age );[/FONT]
[FONT=Arial] break; [/FONT]
[FONT=Arial] case 1: [/FONT]
[FONT=Arial] BMR = 655 + ( 9.6 * wlWeight ) + ( 1.8 * wlHeight2 ) - ( 4.7 * age);[/FONT]
[FONT=Arial] break; [/FONT]
[FONT=Arial] }[/FONT]
[FONT=Arial] }[/FONT]
[FONT=Arial] });[/FONT]
[FONT=Arial] //[/FONT]
[FONT=Arial] double newNum2 = Math.round(BMR*100)/100;[/FONT]
[FONT=Arial] wlbmr=(Double.toString(newNum2));[/FONT]
[FONT=Arial] Intent myIntent = new Intent(View.getContext(), Container.class);[/FONT]
[FONT=Arial] startActivityForResult(myIntent, 0);[/FONT]
[FONT=Arial] }[/FONT]
[FONT=Arial] }[/FONT]
[FONT=Arial] });[/FONT]
[FONT=Arial] ((Button) findViewById(R.id.bntMM)).setOnClickListener(new View.OnClickListener()[/FONT]
[FONT=Arial] {[/FONT]
[FONT=Arial] @Override[/FONT]
[FONT=Arial] public void onClick(View v) {[/FONT]
[FONT=Arial] finish();}[/FONT]
[FONT=Arial] });[/FONT]
[FONT=Arial]}[/FONT]
[FONT=Arial]private void showErrorAlert(String errorMessage, [/FONT]
[FONT=Arial]final int fieldId) {[/FONT]
[FONT=Arial] new AlertDialog.Builder(this).setTitle("Error")[/FONT]
[FONT=Arial] .setMessage(errorMessage).setNeutralButton("Close",[/FONT]
[FONT=Arial] new DialogInterface.OnClickListener() {[/FONT]
[FONT=Arial] @Override[/FONT]
[FONT=Arial] public void onClick(DialogInterface dialog,[/FONT]
[FONT=Arial] int which) {[/FONT]
[FONT=Arial] findViewById(fieldId).requestFocus();[/FONT]
[FONT=Arial] }[/FONT]
[FONT=Arial] }).show();[/FONT]
[FONT=Arial]}[/FONT]
[FONT=Arial]}[/FONT]
[/FONT]
google code site http://code.google.com/p/egoapp/