• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to take data from an edit text and times it against a spinner value - dynamic update?

I'm trying to make a simple order system. So the user selects a burger type from a spinner, then the user can enter in the quantity and then there is some math taking place in the background which sets a text view at the bottom with the total price. I'm not sure what method I'm meant to use so that it is updated dynamically when the user changes the text view quantity.

If i just say that all burgers are 2.50, i need some sort of method saying, if/when data is entered into the edit text , take that data and then times it by 2.50 and set it in the total text view below. I'm just unsure on the method part of it so it's dynamic and will change when the edit text quantity is altered.

Code:
package placeorder.com;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class Food extends Activity {
	
	String[] burgersarray, chipsarray, saladarray;
	Random order = new Random();
	double bquantity, hamburger, burgertotal;
			

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.food);
		
		EditText burgerquantity = (EditText) findViewById(R.id.et_burger);			
		TextView total = (TextView) findViewById(R.id.tv_total);
		Spinner burgers = (Spinner) findViewById(R.id.sp_burgers);		
		
		burgersarray = getResources().getStringArray(R.array.burgers);
		chipsarray = getResources().getStringArray(R.array.chips);
		saladarray = getResources().getStringArray(R.array.salad);		
		TextView orderid = (TextView) findViewById(R.id.tv_orderid);		
		
		int randomorder = order.nextInt(9999);
		orderid.setText("Order ID: " + randomorder);		
		
		
		ArrayAdapter<String> burgersadapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_dropdown_item, burgersarray);

		burgers.setAdapter(burgersadapter);
		
		
		burgers.setOnItemSelectedListener(new OnItemSelectedListener()

		{

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {

				String burger = (String) arg0.getSelectedItem();	
			}

			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});		
		
		
		
		
		
	}
	
	

}
 
I currently have this however I'm unsure how I would add another spinner and add that to the total. So If someone want to order 5 burgers, but then also 3 caesar salads. I've been trying to play with it all evening but just can't figure out how to get it to work with 2 or more spinners.



Code:
package placeorder.com;

import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.text.TextWatcher;

public class Food extends Activity {

	String[] burgersarray, chipsarray, saladarray;
	Random order = new Random();	
	double grandtotal;
	int currentSelection = 0;
	double[] burgerprices = { 5, 3, 2.80, 2.50, 3 };
	double[] chipsprices = { 1.90, 2.40, 2.10 };
	double[] saladprices = { 3.20, 2.80, 2.50, 2.80, 3.00, 3.50 };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.food);
		
		EditText burgerquantity = (EditText) findViewById(R.id.et_burger);
		EditText saladquantity = (EditText) findViewById(R.id.et_burger);

		Spinner burgers = (Spinner) findViewById(R.id.sp_burgers);
		Spinner chips = (Spinner) findViewById(R.id.sp_chips);
		Spinner salad = (Spinner) findViewById(R.id.sp_salad);

		burgersarray = getResources().getStringArray(R.array.burgers);
		chipsarray = getResources().getStringArray(R.array.chips);
		saladarray = getResources().getStringArray(R.array.salad);
		TextView orderid = (TextView) findViewById(R.id.tv_orderid);

		int randomorder = order.nextInt(9999);
		orderid.setText("Order ID: " + randomorder);

		ArrayAdapter<String> burgersadapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_dropdown_item, burgersarray);
		
		ArrayAdapter<String> chipsadapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_dropdown_item, chipsarray);
		
		ArrayAdapter<String> saladsadapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_dropdown_item, saladarray);

		burgers.setAdapter(burgersadapter);
		chips.setAdapter(chipsadapter);
		salad.setAdapter(saladsadapter);

		burgers.setOnItemSelectedListener(new OnItemSelectedListener()

		{

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				EditText burgerquantity = (EditText) findViewById(R.id.et_burger);
				TextView total = (TextView) findViewById(R.id.tv_total);
				currentSelection = arg2;
				// this is needed because the user could enter a value in the
				// EditText but change to another burger category and so you
				// update the total for the new price
				if (!burgerquantity.getText().toString().equals("")) {
					double burgerresult = Integer.parseInt(burgerquantity.getText()
							.toString()) * burgerprices[currentSelection];
					total.setText("
 
You can make a function that updates the price and call that function from each of your OnItemSelectedListener classes for each spinner should do the trick

Code:
/// Function to listen for spinner1 packages
    class Spinner1 implements OnItemSelectedListener {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                        if(parent.getItemAtPosition(pos).toString().equals("None")) {
                
                itemPrice = (float) 0.00;
                
            }
            if(parent.getItemAtPosition(pos).toString().equals("Item 1")) {
                
                itemPrice = (float) 69.99;
                
            }
            if(parent.getItemAtPosition(pos).toString().equals("item 2")) {
                itemPrice = (float) 89.99;
            }
            if(parent.getItemAtPosition(pos).toString().equals("item3")) {
                itemPrice = (float) 109.99;
            }
            
            mItem = parent.getItemAtPosition(pos).toString();
            updatePrice();
        }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            
        }
    }

Code:
/// Function to round price to only (2) decimal places
    public static float Round(float Rval, int Rpl) {
        float p = (float)Math.pow(10, Rpl);
        Rval = Rval * p;
        float tmp = Math.round(Rval);
        return tmp/p;
    }
    /// Function to calculate price
    public void updateDataPrice() {
        itemTotalPrice = item1Price + item2Price + item3Price + item4Price + item5Price;
        updatePrice();
    }
    
    /// Function to calculate total price
    public void updatePrice() {
        TextView price = new TextView(this);
        price = (TextView) findViewById(R.id.pricee);
        totalPrice = item1Price + item2Price + item3TotalPrice;
        float round = Round(totalPrice,2);
        price.setText("$" + round);
        pPrice = ("$" + round);
    }
 
Back
Top Bottom