user1078385
user1078385

Reputation: 344

Android Spinner Array

I have created a Spinner and have populated it with values for my app which will work out alcohol units in drinks.

The spinner hold the different type of measures of drinks Pint, Half Pint, Bottle and Can.

The alcohol units are worked out by talking the volume of the drink which is held in the spinner times by amount of drinks times by alcohol percentage over 1000.

What I want to happen is when the user selects some of the values form the spinner such as pint it will add the volume of a pint into the equation.

This is the code I have for the spinner

int pint,half,bottle,can;
Spinner beerspinner;
Spinner spinner = (Spinner) findViewById(R.id.beerspinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.drinks_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

I want to somehow match the values from the spinner to an integer so I can carry out the equations.

pint = 568;
half = 284;
bottle = 275;
can = 440;

I haven't really explained it very well here is my complete code so you can get a better idea of what I want to be done.

public class AppTestActivity extends Activity {
int pint,half,bottle,can;
Spinner beerspinner;
Button submit, reset;
EditText ABV,quantity;
TextView units, error;


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

ABV = (EditText) findViewById(R.id.ABV);
submit = (Button) findViewById(R.id.submit);
reset = (Button) findViewById(R.id.reset);
units = (TextView) findViewById(R.id.units);
error = (TextView) findViewById(R.id.error);
quantity= (EditText) findViewById(R.id.quantity);

Spinner spinner = (Spinner) findViewById(R.id.beerspinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.drinks_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

submit.setOnClickListener(new clicker());
 }
class clicker implements Button.OnClickListener{

public void onClick(View v){

pint = 568;
half = 284;
bottle = 275;
can = 440;

String number,percentage;
Float calc; 

percentage = ABV.getText().toString();
number = quantity.getText().toString(); 
calc = Float.parseFloat(number)*Float.parseFloat(percentage)/1000;
units.setText(calc.toString());
}

Currently the code takes the number of drinks times it by the alcohol percentage and then divides by 1000 and I need it to add the drink types from the spinner to the equation.

Upvotes: 0

Views: 616

Answers (1)

Anila
Anila

Reputation: 1136

Crate an int in your activity.

int drinkType; 

Use setOnItemSelected listener for the spinner:

spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
      public void on ItemSlected(...)
          int selectedIndex=spinner.getSelectedItenPosition();

          if(selectedIndex==0) //pint for example
                drinkType = 568;
          //do this for other drink types
}

And in your code above you can do

calc = Float.parseFloat(number)*Float.parseFloat(percentage)/1000+ drinkType;

Upvotes: 3

Related Questions