Spinners...

Put your problem here if it does not fit any of the other categories.

Spinners...

Postby braves180 » Sat Feb 04, 2012 10:39 pm

Thaks phyll for your help with the memory game!!... I've been working on a project with spinners... The object is to enter a number and convert it from one type to another.
Image
.I'm not getting the correct answers. My goal is to remove the button and just do the calculation when the 2nd or 3rd spinner item is selected.. I think my problem maybe with the spinner listeners but im not sure(not getting any errors just not getting the correct answers, I know the conversion functions are correct).....
My code is not complete, just not working correctly so far


Code: Select all
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;


public class ConcentrationActivity extends Activity implements OnItemSelectedListener, View.OnClickListener {
/*    private TextView selection;*/

   Button calc;
   EditText myinput;
   TextView myoutput;
   float temp;
   int flag=0;
   int secondFlag=0;
   Spinner spinner,spinner2,spinner3;
   String[] categories = {"Temperature","Liquids","Length/Distance","Area","Weight"};
        String[] temperatures = {"Celsius","Fahrenheit","Kelvin"};
        String[] liquids = {"Teaspoon","Tablespoon","Fl oz","Cups","Pints","Quarts","Gallons"};
   
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, temperatures);
        ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, liquids);
       
        calc = (Button) findViewById(R.id.calculate);
        calc.setOnClickListener(this);
       
        myinput = (EditText) findViewById(R.id.Input);
        myinput.setText("0");
       
        myoutput = (TextView) findViewById(R.id.output);

       
        spinner = (Spinner) findViewById(R.id.spinner1);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);
       
        spinner2 = (Spinner) findViewById(R.id.spinner2);
       spinner2.setAdapter(adapter2);
       spinner2.setOnItemSelectedListener(this);
       
        spinner3 = (Spinner) findViewById(R.id.spinner3);
        spinner3.setAdapter(adapter3);
        spinner3.setOnItemSelectedListener(this);
       
        }


   public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
      // TODO Auto-generated method stub
      int position = spinner.getSelectedItemPosition();
      int position2 = spinner2.getSelectedItemPosition();
      int position3 = spinner3.getSelectedItemPosition();
   
      if (position == 0)
      {
         if (secondFlag == 0 )
         {
            SetDrops();
         }
         secondFlag=1;
         
         if(position2 == 0 && position3 == 1)
         {
            //flag = 0;
            //celtofah(temp);
            myoutput.setText(Float.toString(celtofah(temp)));
         }
         else if (position2 == 0 && position3 == 2)
         {
            //flag = 1;
            //celtokel(temp);
            myoutput.setText(Float.toString(celtokel(temp)));
         }
         else if (position2 == 1 && position3 == 0)
         {
            //flag = 2;
            //fahtocel(temp);
            myoutput.setText(Float.toString(fahtocel(temp)));
            
         }
         else if (position2 == 1 && position3 == 2)
         {
            //flag = 3;
            myoutput.setText(Float.toString(fahtokel(temp)));
            
         }
         else if (position2 == 2 && position3 == 0)
         {
            //flag = 4;
            myoutput.setText(Float.toString(keltocel(temp)));
         }
         else if (position2 == 2 && position3 == 1)
         {
            //flag = 5;
            myoutput.setText(Float.toString(keltofah(temp)));
         }
      }
      else if (position == 1)
      {   
         ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, liquids );
         spinner2 = (Spinner) findViewById(R.id.spinner2);
         spinner2.setAdapter(adapter2);
         spinner2.setOnItemSelectedListener(this);
         
         ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, liquids);
         spinner3 = (Spinner) findViewById(R.id.spinner3);
         spinner3.setAdapter(adapter3);
         spinner3.setOnItemSelectedListener(this);
      }
   }
   

public void SetDrops()
{
   ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, temperatures);
   spinner2 = (Spinner) findViewById(R.id.spinner2);
   spinner2.setAdapter(adapter2);
   //spinner2.setOnItemSelectedListener(this);
   
   ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, temperatures);
   spinner3 = (Spinner) findViewById(R.id.spinner3);
   spinner3.setAdapter(adapter3);
   //spinner3.setOnItemSelectedListener(this);
}
   public void onClick(View v)
   {
      temp = Float.valueOf(myinput.getText().toString());
      float capture;
      if (flag==0)
      {
         celtofah(temp);
         myoutput.setText(Float.toString(celtofah(temp)));
      }
      else if (flag==1)
      {
         celtokel(temp);
         myoutput.setText(Float.toString(celtokel(temp)));
      }
      else if (flag==2)
      {
         fahtocel(temp);
         myoutput.setText(Float.toString(fahtocel(temp)));
      }
      else if (flag==3)
      {
         

         myoutput.setText(Float.toString(fahtokel(temp)));
      }
      else if (flag==4)
      {
         keltocel(temp);
         
         myoutput.setText(Float.toString(keltocel(temp)));
      }
      else if (flag==5)
      {
         capture = keltofah(temp);
         
         myoutput.setText(Float.toString(keltofah(temp)));
      }
   }
   
   
   public float fahtocel(float num)
   {
      float fahrenheit = num,celsius;
      celsius = (fahrenheit-32)*5/9;
      return celsius;
   }
   public float fahtokel(float num)
   {
      float fahrenheit = num,kelvin;
      kelvin = (float) ((fahrenheit -32)*5/9 + 273.15);
      return kelvin;
   }
   public float celtofah(float num)
   {
      float celsius = num,fahrenheit;
      fahrenheit = (celsius * 9/5) + 32;
      return fahrenheit;
   }
   public float celtokel(float num)
   {
      float celsius = num,kelvin;
      kelvin = (float) (celsius + 273.15);
      return kelvin;
   }
   public float keltofah(float num)
   {
      float kelvin = num,fahrenheit;
      fahrenheit = (float) (((kelvin - 273.15) * 9/5) + 32);
      return fahrenheit;
   }
   public float keltocel(float num)
   {
      float kelvin = num,celsius;
      celsius = (float) (kelvin - 273.15);
      return celsius;
   }
   
braves180
Freshman
Freshman
 
Posts: 3
Joined: Fri Jan 27, 2012 2:55 am

Top

Re: Spinners...

Postby Phyll » Sun Feb 05, 2012 6:31 am

Hi braves180,

The problem is, that the flag that gets sent to the decision code never is set. It always arrives as zero and calculates celcius to fahrenheit.

That decision code should look more like this so that someone can look at it and figure it out.

Code: Select all
        temp = Float.valueOf(myinput.getText().toString());
        switch (flag) {
            case 0: myoutput.setText(Float.toString(celtofah(temp)));
            break;
            case 1: myoutput.setText(Float.toString(celtokel(temp)));
            break;
            case 2: myoutput.setText(Float.toString(fahtocel(temp)));
            break;
            case 3: myoutput.setText(Float.toString(fahtokel(temp)));
            break;
            case 4: myoutput.setText(Float.toString(keltocel(temp)));
            break;
            case 5: myoutput.setText(Float.toString(keltofah(temp)));
            break;
            }//switch


The other thing that figures out the flag value from the positons of the spinners is just to complicated for me to take on in the middle of the night. So I guess you'll have to figure out how it was supposed to assign a value to flag.

Anyway, if the flag is 3 when it gets to the decision code with a value of 300 input and Fahrenheit and Kelvin selected, the answer is 422.38 or something like that.

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 20 guests