Have program display results


Last Updated:

  1. gotenks05

    gotenks05 Member This Topic's Starter

    Joined:
    May 5, 2010
    Messages:
    8
    Likes Received:
    0
    I have recently got a Droid and became interested in programming for the Android. I have experience with J2SE and I want to port a certain App that I created to the droid, but I can seem to figure out what is wrong. In the program, the user enter in a number, then selects a unit of measure from a comboBox (Spinner, which is what I learned it was called on Android). Next, the user presses a button a the calculation results come in. The calculation is the actual storage capacity in the range from KB to YB, which formula is determined by the user's settings. The main problem that I have though is that the results are not displayed on the GUI. What do I need to do to correct this?

    My source code looks like this:

    Code (Text):
    1. package com.Actual.android;
    2.  
    3. import android.app.Activity;
    4. import android.os.Bundle;
    5. import android.widget.*;
    6. import android.view.*;
    7. public class ActualStorageActivity extends Activity
    8. {
    9.  
    10.     Spinner selection = (Spinner)findViewById(R.id.spinner); /* declare variable, in order to control spinner (ComboBox) */
    11.     ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.choices_array, android.R.layout.simple_spinner_dropdown_item); /* declare an array adapter object, in order for spinner to work */
    12.     EditText size = (EditText)findViewById(R.id.size); /* declare variable to control textfield */
    13.     EditText result = (EditText)findViewById(R.id.result); /* declare variable to control textfield */
    14.     Button calculate = (Button)findViewById(R.id.submit); /* declare variable to control button */
    15.     Storage capacity = new Storage(); /* import custom class for formulas */
    16.  
    17.     /** Called when the activity is first created. */
    18.     @Override
    19.     public void onCreate(Bundle savedInstanceState)
    20.     {
    21.         super.onCreate(savedInstanceState);
    22.         setContentView(R.layout.main); // load content from XML
    23.  
    24.     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); /* set resource for dropdown */
    25.  
    26.     selection.setAdapter(adapter); // attach adapter to spinner
    27.  
    28.     result.setCursorVisible(false); // hide cursor
    29.  
    30.     calculate.setOnClickListener(new View.OnClickListener() {
    31. public void onClick(View v) {
    32.             String initial = size.getText().toString();
    33.             String unit = (String)selection.getSelectedItem();
    34.             String end;
    35.             double convert = Double.parseDouble(initial);
    36.             capacity.setStorage(convert);
    37.  
    38.             if (unit == "KB")
    39.             {
    40.                 end = Double.toString(capacity.getKB());
    41.                 result.setText(end);
    42.             }
    43.  
    44.             else if (unit == "MB")
    45.             {
    46.                 end = Double.toString(capacity.getMB());
    47.                 result.setText(end);
    48.  
    49.             }
    50.  
    51.             else if (unit == "GB")
    52.             {
    53.                 end = Double.toString(capacity.getGB());
    54.                 result.setText(end);
    55.  
    56.             }
    57.  
    58.             else if (unit == "TB")
    59.             {
    60.                 end = Double.toString(capacity.getTB());
    61.                 result.setText(end);
    62.  
    63.             }
    64.  
    65.             else if (unit == "PB")
    66.             {
    67.                 end = Double.toString(capacity.getPB());
    68.                 result.setText(end);
    69.  
    70.             }
    71.  
    72.             else if (unit == "EB")
    73.             {
    74.                 end = Double.toString(capacity.getEB());
    75.                 result.setText(end);
    76.  
    77.             }
    78.  
    79.             else if (unit == "ZB")
    80.             {
    81.                 end = Double.toString(capacity.getZB());
    82.                 result.setText(end);
    83.  
    84.             }
    85.  
    86.             else if (unit == "YB")
    87.             {
    88.                 end = Double.toString(capacity.getYB());
    89.                 result.setText(end);
    90.  
    91.             }
    92.         } }); /* attach button listener */
    93.     }
    94.        
    95. }
    Here is what the arrays.xml looks like:

    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <resources>
    3.     <string-array name="choices_array">
    4.     <item>KB</item>
    5.     <item>MB</item>
    6.     <item>GB</item>
    7.     <item>TB</item>
    8.     <item>PB</item>
    9.     <item>EB</item>
    10.     <item>ZB</item>
    11.     <item>YB</item>
    12.     </string-array>
    13. </resources>
    Here is what the strings.xml looks like:

    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <resources>
    3.     <string name="app_name">Actual Storage</string>
    4.     <string name="choices">Select a unit</string>
    5.     <string name="message">Enter a Size</string>
    6.     <string name="submit_text">Calculate</string>
    7. </resources>
    Here is what main.xml looks like:

    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.     android:orientation="vertical"
    4.     android:layout_width="fill_parent"
    5.     android:layout_height="fill_parent"
    6.     >
    7.  
    8. <!-- the following creates a textfield -->
    9. <EditText android:id="@+id/size"
    10.     android:layout_width="fill_parent"
    11.     android:layout_height="wrap_content"
    12. />
    13.  
    14. <!-- the following creates a combobox -->
    15. <Spinner android:id="@+id/spinner"
    16.     android:layout_width="fill_parent"
    17.     android:layout_height="wrap_content"
    18.     android:drawSelectorOnTop="true"
    19.     android:prompt="@string/choices"
    20. />
    21.  
    22. <!-- the following creates another textfield -->
    23. <EditText android:id="@+id/result"
    24.     android:layout_width="fill_parent"
    25.     android:layout_height="wrap_content"
    26. />
    27.  
    28. <!-- the following creates a button -->
    29. <Button android:id="@+id/submit"
    30.     android:layout_width="fill_parent"
    31.     android:layout_height="wrap_content"
    32.     android:text="@string/submit_text"
    33. />
    34. </LinearLayout>
    No matter whether I use Toast or a readonly EditText, I can't display the results. This is my first time Programming for the Android and the official tutorial does not have anything on this, or give much help about it.
     

    Advertisement

Share This Page

Loading...