Usar más de un Spinner en un cálculo

Ok, so I'm trying to create an activity that works out the area of a given space.

I want the user to be able to enter the dimension in either "ft" of "m". The answer is required to be given in meters at the end.

Right, now to the problem.

The below code works, the problem is that the program assumes that both num1 and num2 are both equal to what ever is selected in the last spinner used. So if you input 100 ft for num1 then 10m for num2 it gives the answer of 1000m, because you select the m unit last. It should be giving the answer of 92.9...

This has been a headache for the last few days...

Can anyone help me!?!

Thanks in advance Will

private OnClickListener myClickListener = new OnClickListener()
{
    public void onClick(View v) {
        try{

        a=Double.parseDouble(num1.getText().toString());
        b=Double.parseDouble(num2.getText().toString());}
        catch(Exception e)
        {
            if(num1.getText().length()==0)
            {
                num1.setError("please input width");
            }
            if(num2.getText().length()==0)
            {
                num2.setError("please input length");
            }

        }

        if (opselected=="ft" && opselected1=="ft")
        {c=((a * 0.0929) * (b * 0.0929));tv1.setText(Double.toString(c));}
        else if (opselected=="m" && opselected1=="m")
        {c=( a * b);tv1.setText(Double.toString(c));}
        else if (opselected=="m" && opselected1=="ft")
        {c= (a * (b * 0.0929));tv1.setText(Double.toString(c));}
        else if (opselected=="ft" && opselected1=="m")
        {c= ((a * 0.0929) * b);tv1.setText(Double.toString(c));}



        else {tv1.setText("select units");}
        //tv1 = (TextView)findViewById(R.id.TextView01);


    }
};
    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.car);

       tv1 = (TextView)findViewById(R.id.TextView01);


       button01 = (Button)findViewById(R.id.Button01);
       button01.setText("Display Air Volume");
       button01.setOnClickListener(myClickListener);



       num1 = (EditText)findViewById(R.id.EditText01);
       num2 = (EditText)findViewById(R.id.EditText02);



       spinOps = (Spinner)findViewById(R.id.Spinner01);
       ArrayAdapter<String> adapter = new ArrayAdapter<String>(
         this, android.R.layout.simple_spinner_item, ops);
       adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       spinOps.setAdapter(adapter);
       spinOps.setOnItemSelectedListener(this);

       spinOps1 = (Spinner)findViewById(R.id.Spinner02);
       ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(
         this, android.R.layout.simple_spinner_item, ops1);
       adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       spinOps1.setAdapter(adapter);
       spinOps1.setOnItemSelectedListener(this);

    }   
    public void onItemSelected (AdapterView<?> p,View v,int position,long id) {
        opselected=ops[position];
        opselected1=ops1[position];
        tv1.setText("");
    }
    public void onNothingSelected(AdapterView<?> p) { 
        tv1.setText(""); 
    }

preguntado el 01 de julio de 12 a las 16:07

Can anyone help by showing how i need to amend the above code so this would work? -

2 Respuestas

Give each spinner its OWN listener. For example, where you have

  spinOps.setOnItemSelectedListener(this);

Replace that with something more like this:

  spinOps.setOnItemSelectedListener(new onItemSelectedListener() {
      public void onItemSelected(AdapterView<?> p, View v, int position, long id) {
         // get the value entered and the unit selected (e.g., val0, unit0)
      }
  });

Do the same (except using val1 and unit1, and it would be spinOps1....) and if the units are different, pick one and convert the other to match. In other words, if the user selects 39 inches for the first, and 2 meters for the second, and you want it in meters, 39 inches = 1 m, so the result would be 3 meters.

Ver http://developer.android.com/reference/android/widget/AdapterView.OnItemSelectedListener.html for more information. (Just search for onItemSelectedListener and take the first result....)

Later, --jim

PS: yes, I know, I used inches instead of feet ... keeping it simple :-)

Respondido 02 Jul 12, 03:07

Jim, thanks for the pointer and I'm sure you are keeping it simple but still completely lost with this... could you help show how I could implement it into the above? - Testamento

Doing it the way I showed you, you have a listener for each spinner. You don't need to try to sort out which gave which value, or which units it was in. They are two distinct results, and you can assign different variables for each, e.g., res1 and unit1, and res2 and unit2 for results and units. Then convert one or the other, as needed, so they're both the same units. You can search in the Android Developers list archives for conversations that give examples of this---that's how I originally learned that I could do it this way. I've forgotten the keywords I searched with...sorry. - cervecero

Just FYI, you can also put the two spinners in the same spot (using a RelativeLayout) and control which (if either) is on the screen by changing each spinner's visibility: foo.setVisibility(View.VISIBLE) or foo.setVisibility(View.INVISIBLE). Then, when you're done with the first one, set it to invisible and the second to visible, then when you're done with the second, you can set it to invisible, too. - cervecero

in onItemSelected(), you can use v.getId() to see if it was called for R.id.Spinner01 or R.id.Spinner02, and update either opselected OR opselected1, not both. for example:

public void onItemSelected (AdapterView<?> p,View v,int position,long id) {
    if (v.getId() == R.id.Spinner01)
        opselected=ops[position];
    else if (v.getId() == R.id.Spinner02)
        opselected1=ops1[position];
    tv1.setText("");
}

Respondido 01 Jul 12, 17:07

hmm, havent come across this function yet... I'm still very new to this. any chance you could help by showing how i could implement this to my code? - Testamento

hmmm, instead of giving an answer, it now just tells me to select units. Any ideas (thanks for your input so far anyway Steelight) - Testamento

Don't use == with String, use .equals() - using == compares the pointer, not the content. - luz de acero

Argh, no go Steelight, this newest change now causes the application to crash out when you hit the button. Thanks for your help tho!! - Testamento

add a null check, or initialize the value. - luz de acero

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.