• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Have program display results

gotenks05

Lurker
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:
package com.Actual.android;

import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.view.*;
public class ActualStorageActivity extends Activity
{

	Spinner selection = (Spinner)findViewById(R.id.spinner); /* declare variable, in order to control spinner (ComboBox) */
	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 */
	EditText size = (EditText)findViewById(R.id.size); /* declare variable to control textfield */
	EditText result = (EditText)findViewById(R.id.result); /* declare variable to control textfield */
	Button calculate = (Button)findViewById(R.id.submit); /* declare variable to control button */
	Storage capacity = new Storage(); /* import custom class for formulas */

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); // load content from XML

	adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); /* set resource for dropdown */

	selection.setAdapter(adapter); // attach adapter to spinner

	result.setCursorVisible(false); // hide cursor

	calculate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
			String initial = size.getText().toString();
			String unit = (String)selection.getSelectedItem();
			String end;
			double convert = Double.parseDouble(initial);
			capacity.setStorage(convert);

			if (unit == "KB")
			{
				end = Double.toString(capacity.getKB());
				result.setText(end);
			}

			else if (unit == "MB")
			{
				end = Double.toString(capacity.getMB());
				result.setText(end);

			}

			else if (unit == "GB")
			{
				end = Double.toString(capacity.getGB());
				result.setText(end);

			}

			else if (unit == "TB")
			{
				end = Double.toString(capacity.getTB());
				result.setText(end);

			}

			else if (unit == "PB")
			{
				end = Double.toString(capacity.getPB());
				result.setText(end);

			}

			else if (unit == "EB")
			{
				end = Double.toString(capacity.getEB());
				result.setText(end);

			}

			else if (unit == "ZB")
			{
				end = Double.toString(capacity.getZB());
				result.setText(end);

			}

			else if (unit == "YB")
			{
				end = Double.toString(capacity.getYB());
				result.setText(end);

			}
		} }); /* attach button listener */
	}
		
}

Here is what the arrays.xml looks like:

Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="choices_array">
	<item>KB</item>
	<item>MB</item>
	<item>GB</item>
	<item>TB</item>
	<item>PB</item>
	<item>EB</item>
	<item>ZB</item>
	<item>YB</item>
    </string-array>
</resources>

Here is what the strings.xml looks like:

Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Actual Storage</string>
    <string name="choices">Select a unit</string>
    <string name="message">Enter a Size</string>
    <string name="submit_text">Calculate</string>
</resources>

Here is what main.xml looks like:

Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<!-- the following creates a textfield -->
<EditText android:id="@+id/size"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
/>

<!-- the following creates a combobox -->
<Spinner android:id="@+id/spinner"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:drawSelectorOnTop="true"
	android:prompt="@string/choices"
/>

<!-- the following creates another textfield -->
<EditText android:id="@+id/result"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
/>

<!-- the following creates a button -->
<Button android:id="@+id/submit"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="@string/submit_text"
/>
</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.
 
Back
Top Bottom