views:

898

answers:

4

I have a spinner 'aperture' set up with a list of numbers, and a spinner 'mode' with two options. When a button is pushed I need a calculation to run using various inputs, including the current selection from 'aperture' and a value derived from 'mode'. How do I call the value of a spinner so I can use it in a calculation?

Also, how do I use the spinner 'mode's selection to set this other value before implimenting it in the calculation? To be more specific, if the spinner is set to Small then the value I use in the calculation is 0.015, whereas if Large is selected I need to use 0.028

My other inputs are EditText views, so right now I am set up like this:

    input = (EditText) findViewById(R.id.input1); 
    input2 = (EditText) findViewById(R.id.input2);
    input3 = (EditText) findViewById(R.id.input3); 
    input4 = (EditText) findViewById(R.id.input4);
    output = (TextView) findViewById(R.id.result); 
    output2 = (TextView) findViewById(R.id.result2);

    //aperture dropdown
    Spinner s = (Spinner) findViewById(R.id.apt);
    ArrayAdapter adapter2 = ArrayAdapter.createFromResource(        this, R.array.apertures,       
    android.R.layout.simple_spinner_item);
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s.setAdapter(adapter2);

    //button
    final Button button = (Button) findViewById(R.id.calculate);
    button.setOnClickListener(new View.OnClickListener() {             
     public void onClick(View v) {                 
      // Perform action on click
      doCalculation();
       }         
      });
     }

private void doCalculation() { 
    // Get entered input value 
    String strValue = input.getText().toString(); 
    String strValue2 = input2.getText().toString();
    String strValue3 = input3.getText().toString(); 
    String strValue4 = input4.getText().toString();

    // Perform a hard-coded calculation 
    double imperial1 = (Double.parseDouble(strValue3) + (Double.parseDouble(strValue4) / 12));
    double number = Integer.parseInt(strValue) * 2; 
    double number2 = ((number / 20) + 3) / Integer.parseInt(strValue2);

    // Update the UI with the result 
    output.setText("Result:  "+ number); 
    output2.setText("Result2: "+ imperial1); 
}
}

That is not the actual equation, it is just a test to make sure everything connects properly. How would I call the value of spinner 'aperture' and the Small/Large spinner 'mode'

+1  A: 

To get the value of the spinner call one of the following as appropriate:

Object item = spinner.getSelectedItem();

long id = spinner.getSelectedItemId();

int position = getSelectedItemPosition();

View selectedView = getSelectedView();

In your case, you could declare the spinner as final and pass the selected item into the doCalculations() method as follows:

    final Spinner aptSpinner = (Spinner) findViewById(R.id.apt);
    ...

    //button
    final Button button = (Button) findViewById(R.id.calculate);
    button.setOnClickListener(new View.OnClickListener() {             
     public void onClick(View v) {                 
      // Perform action on click
      doCalculation(aptSpinner.getSelectedItem());
       }         
      });
     }

    private void doCalculation(Object selectedItem) { 

        ...

    }

It would probably be good for you to pick up a basic Java book and learn how scope in Java works.

Jay Askren
Thanks for the reply. I'm very new to Java, so I apologize. I believe that Object item = spinner.getSelectedItem(); is the correct one, but how do I impliment it? When I try to call it from inside the calculation, Eclipse says it cannot resolve 'aperture'. It works fine if I put the code outside of the calculation but then inside the calculation it can't resolve the object name (in your example "item")
Sean
Thanks for the update. Eclipse doesn't show any errors, but when I have "Object item = aperture.getSelectedItem();" (the spinner is named aperture, not aptSpinner as in your example) in the calculation, it causes the app to crash when I click the button. What might cause this?Sorry again for all the questions. I know I should be learning java first and not jumping in headfirst like this, but I need this app for a film shoot this weekend. Needless to say, I'll be getting some Java books soon anyway :)
Sean
what is the exception?
Jay Askren
A: 

Anyone find an answer to this question I am have the same problem?

Jmsdev
A: 

If R.array.apertures is a String Array, then you can use

Object item = spinner.getSelectedItem();

if(item != null) {

String SelectedString = item.toString();

}

If its any other object say Person.

then

if(item != null) {

Person SelectedString = (Person) item;

}

franklins
A: 

Why not fill your ArrayAdapter programmatically with objects of a known type and use that. I've written a tutorial of a similar nature (link at the bottom) that does this. The basic premise is to create an array of Java objects, tell the spinner about the, and then use those objects directly from the spinner class. In my example I have an object representing a "State" which is defined as follows:

package com.katr.spinnerdemo;

public class State {

// Okay, full acknowledgment that public members are not a good idea, however
// this is a Spinner demo not an exercise in java best practices.
public int id = 0;
public String name = "";
public String abbrev = "";

// A simple constructor for populating our member variables for this tutorial.
public State( int _id, String _name, String _abbrev )
{
    id = _id;
    name = _name;
    abbrev = _abbrev;
}

// The toString method is extremely important to making this class work with a Spinner
// (or ListView) object because this is the method called when it is trying to represent
// this object within the control.  If you do not have a toString() method, you WILL
// get an exception.
public String toString()
{
    return( name + " (" + abbrev + ")" );
}
}

Then you can populate a spinner with an array of these classes as follows:

       // Step 1: Locate our spinner control and save it to the class for convenience
    //         You could get it every time, I'm just being lazy...   :-)
    spinner = (Spinner)this.findViewById(R.id.Spinner01);

    // Step 2: Create and fill an ArrayAdapter with a bunch of "State" objects
    ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
          android.R.layout.simple_spinner_item, new State[] {   
                new State( 1, "Minnesota", "MN" ), 
                new State( 99, "Wisconsin", "WI" ), 
                new State( 53, "Utah", "UT" ), 
                new State( 153, "Texas", "TX" ) 
                });

    // Step 3: Tell the spinner about our adapter
    spinner.setAdapter(spinnerArrayAdapter);  

You can retrieve the selected item as follows:

State st = (State)spinner.getSelectedItem();

And now you have a bonafied java class to work with. If you want to intercept when the spinner value changes, just implement the OnItemSelectedListener and add the appropriate methods to handle the events.

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
{
    // Get the currently selected State object from the spinner
    State st = (State)spinner.getSelectedItem();

    // Now do something with it.
} 

public void onNothingSelected(AdapterView<?> parent ) 
{ 
}

You can find the whole tutorial here: http://www.katr.com/article_android_spinner01.php