Spinner problem


Last Updated:

  1. EGODroid

    EGODroid New Member This Topic's Starter

    Joined:
    Mar 9, 2010
    Messages:
    1
    Likes Received:
    0
    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.

    Code (Text):
    1.  
    2. [FONT=Arial][FONT=Arial]package com.FYP.EGO;[/FONT]
    3.  
    4. [FONT=Arial]import java.util.ArrayList;[/FONT]
    5. [FONT=Arial]import java.util.List;[/FONT]
    6. [FONT=Arial]import android.app.Activity;[/FONT]
    7. [FONT=Arial]import android.app.AlertDialog;[/FONT]
    8. [FONT=Arial]import android.content.DialogInterface;[/FONT]
    9. [FONT=Arial]import android.content.Intent;[/FONT]
    10. [FONT=Arial]import android.os.Bundle;[/FONT]
    11. [FONT=Arial]import android.view.View;[/FONT]
    12. [FONT=Arial]import android.widget.AdapterView;[/FONT]
    13. [FONT=Arial]import android.widget.ArrayAdapter;[/FONT]
    14. [FONT=Arial]import android.widget.Button;[/FONT]
    15. [FONT=Arial]import android.widget.Spinner;[/FONT]
    16. [FONT=Arial]import android.widget.EditText;[/FONT]
    17. [FONT=Arial]import android.widget.TextView;[/FONT]
    18. [FONT=Arial]import android.widget.AdapterView.OnItemSelectedListener;[/FONT]
    19. [FONT=Arial]public class WLactivity1 extends Activity {[/FONT]
    20.  
    21.  
    22. [FONT=Arial]private Spinner spnGender;[/FONT]
    23. [FONT=Arial]private Spinner spnFreq;[/FONT]
    24. [FONT=Arial]private EditText txtwlHeight;[/FONT]
    25. [FONT=Arial]private EditText txtwlWeight;[/FONT]
    26. [FONT=Arial]private EditText txtwlHeight2;[/FONT]
    27. [FONT=Arial]private EditText txtageinput01;[/FONT]
    28. [FONT=Arial]private double wlHeightM ;[/FONT]
    29. [FONT=Arial]private double wlHeightC ;[/FONT]
    30. [FONT=Arial]private double wlHeight ;[/FONT]
    31. [FONT=Arial]private double wlWeight ;[/FONT]
    32. [FONT=Arial]private double wlHeight2;[/FONT]
    33. [FONT=Arial]private int age;[/FONT]
    34. [FONT=Arial]private Button btnWlnext;[/FONT]
    35. [FONT=Arial]private static double bmiresult;[/FONT]
    36. [FONT=Arial]public static String wlbmi;[/FONT]
    37. [FONT=Arial]public static String bmiclass;[/FONT]
    38. [FONT=Arial]public static String wlbmr;[/FONT]
    39. [FONT=Arial]public static String s;[/FONT]
    40. [FONT=Arial]private double BMR;[/FONT]
    41.  
    42.  
    43.  
    44. [FONT=Arial]public void onCreate(Bundle savedInstanceState) {[/FONT]
    45. [FONT=Arial]  super.onCreate(savedInstanceState);[/FONT]
    46. [FONT=Arial]       setContentView(R.layout.main2);[/FONT]
    47. [FONT=Arial]       {[/FONT]
    48. [FONT=Arial]       spnGender= (Spinner)findViewById(R.id.spnGender);[/FONT]
    49. [FONT=Arial]       spnFreq = (Spinner) findViewById(R.id.spnFreq);[/FONT]
    50. [FONT=Arial]       txtwlHeight = (EditText) findViewById(R.id.heightinput01);[/FONT]
    51. [FONT=Arial]       txtwlHeight2 = (EditText) findViewById(R.id.heightinput02);[/FONT]
    52. [FONT=Arial]       txtwlWeight = (EditText) findViewById(R.id.weightinput01);[/FONT]
    53. [FONT=Arial]       txtageinput01 = (EditText) findViewById(R.id.ageinput01);[/FONT]
    54. [FONT=Arial]       btnWlnext = (Button) findViewById(R.id.btnWLnext);[/FONT]
    55. [FONT=Arial]       //this disables the next button when the activity starts[/FONT]
    56. [FONT=Arial]      // btnWlnext.setEnabled(false);[/FONT]
    57.  
    58. [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]
    59.  
    60.  
    61. [FONT=Arial]}[/FONT]
    62.  
    63.  
    64. [FONT=Arial]         //adapting the spinner to use the list array[/FONT]
    65. [FONT=Arial]        ArrayAdapter<CharSequence> Gender= ArrayAdapter.createFromResource(this,R.array.genders, android.R.layout.simple_spinner_item);[/FONT]
    66. [FONT=Arial]        Gender.setDropDownViewResource[/FONT]
    67. [FONT=Arial]        (android.R.layout.simple_spinner_dropdown_item);[/FONT]
    68. [FONT=Arial]        spnGender.setAdapter(Gender);[/FONT]
    69.  
    70. [FONT=Arial]        //adapting the spinner to use the list array[/FONT]
    71. [FONT=Arial]        ArrayAdapter<CharSequence> adapter1= ArrayAdapter.createFromResource(this,R.array.frequency, android.R.layout.simple_spinner_item);[/FONT]
    72. [FONT=Arial]        adapter1.setDropDownViewResource[/FONT]
    73. [FONT=Arial]        (android.R.layout.simple_spinner_dropdown_item);[/FONT]
    74. [FONT=Arial]        spnFreq.setAdapter(adapter1);[/FONT]
    75.  
    76. [FONT=Arial]        // Next button that has multiple commands[/FONT]
    77.  
    78. [FONT=Arial]        btnWlnext.setOnClickListener(new View.OnClickListener()[/FONT]
    79. [FONT=Arial]        {[/FONT]
    80.  
    81. [FONT=Arial]       public void onClick(View View) [/FONT]
    82.  
    83. [FONT=Arial]          {[/FONT]
    84.  
    85. [FONT=Arial]         //boolean isError = false;[/FONT]
    86.  
    87. [FONT=Arial]         if ((txtwlHeight.getText().toString()== " " )[/FONT]
    88. [FONT=Arial]          ||(txtwlHeight.getText().length()== 0 )[/FONT]
    89. [FONT=Arial]          ||(txtwlHeight2.getText().toString()== " ")[/FONT]
    90. [FONT=Arial]          ||(txtwlHeight2.getText().length()== 0 )[/FONT]
    91. [FONT=Arial]          ||(txtwlWeight.getText().toString()== " ")[/FONT]
    92. [FONT=Arial]          ||(txtwlWeight.getText().length()== 0)[/FONT]
    93. [FONT=Arial]          ||(txtageinput01.getText().toString()== " ")[/FONT]
    94. [FONT=Arial]          ||(txtageinput01.getText().length()== 0)[/FONT]
    95. [FONT=Arial]         )[/FONT]
    96. [FONT=Arial]         {[/FONT]
    97. [FONT=Arial]          showErrorAlert("Some Input are empty!", [/FONT]
    98. [FONT=Arial]            txtwlHeight.getId());[/FONT]
    99.  
    100. [FONT=Arial]          }[/FONT]
    101.  
    102. [FONT=Arial]          else [/FONT]
    103.  
    104. [FONT=Arial]          {[/FONT]
    105. [FONT=Arial]                 //converting numbers string[/FONT]
    106. [FONT=Arial]           wlHeightM=Double.parseDouble(txtwlHeight.getText().toString());[/FONT]
    107. [FONT=Arial]                 wlHeightC=Double.parseDouble(txtwlHeight2.getText().toString());[/FONT]
    108. [FONT=Arial]                 wlWeight=Double.parseDouble(txtwlWeight.getText().toString());[/FONT]
    109. [FONT=Arial]                 age=(int) Double.parseDouble(txtageinput01.getText().toString());[/FONT]
    110. [FONT=Arial]                 // convert to meters[/FONT]
    111. [FONT=Arial]                 wlHeight = ((wlHeightM*100)+wlHeightC)/100;[/FONT]
    112. [FONT=Arial]                 //covert to CM[/FONT]
    113. [FONT=Arial]                   wlHeight2=((wlHeightM*100)+wlHeightC);[/FONT]
    114. [FONT=Arial]          bmiresult = wlWeight/(wlHeight*wlHeight);[/FONT]
    115. [FONT=Arial]          //round to 2 decimal places[/FONT]
    116. [FONT=Arial]          double newNum = Math.round(bmiresult*100.0)/100.0;[/FONT]
    117.  
    118. [FONT=Arial]          wlbmi=(Double.toString(newNum));[/FONT]
    119.  
    120. [FONT=Arial]          if (newNum < 18.5 ) [/FONT]
    121. [FONT=Arial]          {[/FONT]
    122. [FONT=Arial]           bmiclass = "Underweight";[/FONT]
    123. [FONT=Arial]          }[/FONT]
    124. [FONT=Arial]          if (newNum > 18.6)[/FONT]
    125. [FONT=Arial]          {[/FONT]
    126. [FONT=Arial]           bmiclass = "Normal weight";[/FONT]
    127. [FONT=Arial]          }[/FONT]
    128.  
    129. [FONT=Arial]          if (newNum > 25)[/FONT]
    130. [FONT=Arial]          {[/FONT]
    131. [FONT=Arial]           bmiclass = "Overweight";[/FONT]
    132. [FONT=Arial]          }[/FONT]
    133. [FONT=Arial]          if (newNum > 30)[/FONT]
    134. [FONT=Arial]          {[/FONT]
    135. [FONT=Arial]           bmiclass = "Obesity (Class 1)";[/FONT]
    136. [FONT=Arial]          }[/FONT]
    137. [FONT=Arial]          if (newNum > 35)[/FONT]
    138. [FONT=Arial]          {[/FONT]
    139. [FONT=Arial]           bmiclass = "Obesity (Class 2)";[/FONT]
    140. [FONT=Arial]          }[/FONT]
    141. [FONT=Arial]          if (newNum > 40)[/FONT]
    142. [FONT=Arial]          {[/FONT]
    143. [FONT=Arial]           bmiclass = "Morbid Obesity";[/FONT]
    144. [FONT=Arial]          }[/FONT]
    145.  
    146. [FONT=Arial]          //BMR formula [/FONT]
    147. [FONT=Arial]          //[/FONT]
    148. [FONT=Arial]          //setting a call back for spinner[/FONT]
    149. [FONT=Arial]                spnGender.setOnItemSelectedListener(new OnItemSelectedListener(){[/FONT]
    150.  
    151. [FONT=Arial]           public void onNothingSelected(AdapterView<?> arg0){}[/FONT]
    152. [FONT=Arial]           @Override[/FONT]
    153. [FONT=Arial]           public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {[/FONT]
    154. [FONT=Arial]            // TODO Auto-generated method stub{ [/FONT]
    155. [FONT=Arial]             switch (position){[/FONT]
    156. [FONT=Arial]                 case 0: [/FONT]
    157. [FONT=Arial]                         BMR = 66 + ( 13.7 * wlWeight ) + ( 5 * wlHeight2 ) - ( 6.8 * age );[/FONT]
    158. [FONT=Arial]                     break; [/FONT]
    159. [FONT=Arial]                 case 1: [/FONT]
    160. [FONT=Arial]                      BMR = 655 + ( 9.6 * wlWeight ) + ( 1.8 * wlHeight2 ) - ( 4.7 * age);[/FONT]
    161. [FONT=Arial]                     break; [/FONT]
    162. [FONT=Arial]            }[/FONT]
    163.  
    164.  
    165. [FONT=Arial]           }[/FONT]
    166. [FONT=Arial]                });[/FONT]
    167. [FONT=Arial]          //[/FONT]
    168.  
    169. [FONT=Arial]          double newNum2 = Math.round(BMR*100)/100;[/FONT]
    170. [FONT=Arial]          wlbmr=(Double.toString(newNum2));[/FONT]
    171.  
    172.  
    173. [FONT=Arial]          Intent myIntent = new Intent(View.getContext(), Container.class);[/FONT]
    174. [FONT=Arial]                  startActivityForResult(myIntent, 0);[/FONT]
    175. [FONT=Arial]          }[/FONT]
    176.  
    177.  
    178. [FONT=Arial]          }[/FONT]
    179.  
    180. [FONT=Arial]        });[/FONT]
    181.  
    182.  
    183. [FONT=Arial]        ((Button) findViewById(R.id.bntMM)).setOnClickListener(new View.OnClickListener()[/FONT]
    184. [FONT=Arial]        {[/FONT]
    185.  
    186. [FONT=Arial]        @Override[/FONT]
    187. [FONT=Arial]   public void onClick(View v) {[/FONT]
    188.  
    189. [FONT=Arial]         finish();}[/FONT]
    190.  
    191.  
    192.  
    193.  
    194. [FONT=Arial]        });[/FONT]
    195.  
    196. [FONT=Arial]}[/FONT]
    197.  
    198. [FONT=Arial]private void showErrorAlert(String errorMessage, [/FONT]
    199. [FONT=Arial]final int fieldId) {[/FONT]
    200. [FONT=Arial]      new AlertDialog.Builder(this).setTitle("Error")[/FONT]
    201. [FONT=Arial]      .setMessage(errorMessage).setNeutralButton("Close",[/FONT]
    202. [FONT=Arial]              new DialogInterface.OnClickListener() {[/FONT]
    203. [FONT=Arial]                  @Override[/FONT]
    204. [FONT=Arial]              public void onClick(DialogInterface dialog,[/FONT]
    205. [FONT=Arial]                         int which) {[/FONT]
    206. [FONT=Arial]                    findViewById(fieldId).requestFocus();[/FONT]
    207. [FONT=Arial]                  }[/FONT]
    208. [FONT=Arial]              }).show();[/FONT]
    209. [FONT=Arial]}[/FONT]
    210. [FONT=Arial]}[/FONT]
    211. [/FONT]
    google code site http://code.google.com/p/egoapp/
     

    Advertisement

Share This Page

Loading...