0

I need some spinner help. I have three spinners, as seen in the XML screenshot I uploaded, that I need to be able to get values from for use in a formula. The formula is

MSF cost = 1000/(width/12*length) * Thickness

The values of width, length, and thickness are selectable in spinners of the same name. I do not know, and have not found a good guide online, for being able to take the selected spinner value and put it into a variable to use in the above formula. enter image description here

    public class Rotary extends Activity {



    private double rollCost; // Entered Price of the Roll
    private double thickness; // selected thickness
    private double width; // selected width
    private double length; // selected length
    private double MSF; //square foot price

    EditText total; // textbox to show total value
    EditText rollPrice; //Textbox for entering price
    Spinner slength;
    Spinner swidth;
    Spinner sthick;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rotary);



        rollPrice = (EditText) findViewById(R.id.RollPrice);
        total = (EditText) findViewById(R.id.total);
        sthick = (Spinner) findViewById(R.id.Thick_spinner);
        swidth = (Spinner) findViewById(R.id.Width_spinner);
        slength = (Spinner) findViewById(R.id.length_spinner);

        //rollPrice.addTextChangedListener(rollPriceListener);
       swidth = (Spinner) findViewById(R.id.Width_spinner);
       sthick = (Spinner) findViewById(R.id.Thick_spinner);
       slength = (Spinner) findViewById(R.id.length_spinner);




        TextWatcher rollPriceListener = new TextWatcher(){

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {

                try{

                    // Change the billBeforeTip to the new input

                    rollCost = Double.parseDouble(arg0.toString());

                }

                catch(NumberFormatException e){

                    rollCost = 0.0;

                }




        };
};
user2727048
  • 115
  • 2
  • 12

2 Answers2

0

Spinner already has a builtin function to do that.

I did basic Google search and found these:

https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&es_th=1&ie=UTF-8#q=spinner+get+selected+value&safe=off

How do you get the selected value of a Spinner?

Community
  • 1
  • 1
tasomaniac
  • 10,234
  • 6
  • 52
  • 84
  • i did a basic google search too and read some of those examples, my problem is converting it over to my own code. do I need a method for each spinner or just one for all? I dont need anyone to do the code for me just something to get me started in the right direction – user2727048 Jul 22 '14 at 03:18
0

Write below line, when you want to get value from spinner:

String value = YOUR_SPINNER.getSelectedItem().toString();
Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44
  • do I put 3 of them together and changethe string name? Does that take the number pressed and put it into a variable I can then use again for the formula I put above? – user2727048 Jul 22 '14 at 03:53
  • Yes declare 3 String variable and obtain selected values in them. – Pratik Dasa Jul 22 '14 at 03:54
  • I am very new at this and i am teaching myself so it is not easy for me to follow. so I can put String value = width_spinner.getSelectedItem().toString(); and declare String width; so now where to I put that code, just randomly inside oncreate? – user2727048 Jul 22 '14 at 03:57
  • ya then just do like above when you want your value, its just one line code, so I dont think it will take more time to learn, just try to put by your self and learn new things. – Pratik Dasa Jul 22 '14 at 03:59
  • String B5 = swidth.getSelectedItem().toString(); String B6 = slength.getSelectedItem().toString(); String B4 = sthick.getSelectedItem().toString(); double MSF = 1000/(B5/12*B6)*B3; I know that is wrong but that is all I have... – user2727048 Jul 22 '14 at 04:12
  • 1
    MSF=1000/(Double.parseDouble(B5)/12*Double.parseDouble(B6))*Double.parseDouble(B3); – Pratik Dasa Jul 22 '14 at 04:13