Grace D
Grace D

Reputation: 11

Selecting Spinner with a variable,

I am new to Android. I am trying to select the spinner choice and multiply it by the user input. (it's an app that multiples the number of pizzas by the cost of each pizza) Does that make sense? So, I am able to process one spinner option and get the correct answer, but i can't figure out how do code more than one spinner option.

Heres the Java:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import java.text.DecimalFormat;

public class MainActivity extends AppCompatActivity {

    //below is where I have placed the values associated with the Pizza Choices//
    //each one is under the txtPizza string array and is referenced//

    double costCheese = 4.50;
    double costPepperoni = 5.75;
    double costVeggie = 4.75;
    double costGluten = 5.30;
    double costPersonal = 3.15;

    int txtnumber;
    double txtresult;

    String txtGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    final EditText amount = (EditText)findViewById(R.id.txtnumber);
    final Spinner type =(Spinner)findViewById(R.id.txtGroup);


        Button cost = (Button)findViewById(R.id.OrderBtn);
        cost.setOnClickListener(new View.OnClickListener() {
            final TextView result = ((TextView)findViewById(R.id.txtResult));
            @Override
            public void onClick(View v) {
                txtnumber = Integer.parseInt(amount.getText().toString());
                txtresult = costCheese * txtnumber;
                DecimalFormat currency = new DecimalFormat("$###,###.##");
                result.setText("Cost for this order is " + currency.format(txtresult)); }




        });



    }}

Upvotes: 1

Views: 58

Answers (1)

mariuss
mariuss

Reputation: 1247

Well you need an adapter to illustrate the options that you desire.

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
array_spinner=new String[5];
array_spinner[0]="option1";
array_spinner[1]="option2";
array_spinner[2]="option3";
array_spinner[3]="option4";
array_spinner[4]="option5";
Spinner s = (Spinner) findViewById(R.id.Spinner01);
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, array_spinner);
s.setAdapter(adapter);
}

Check this out for more about spinners.

Upvotes: 1

Related Questions