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

Apps Simple...Selecting different spinner option doesn't update calculation

Hey guys,

I have a simple spinner which has several number options. I want to just select a number and then it does some math to that number and outputs it in a text box. For some reason it's only doing math on the 1st entry in the list and if you change it doesn't update.

Any ideas?

Code:
package placeorder.com;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Spinner;
import android.widget.TextView;

public class Time extends Activity{	
	
	double totalhours, cost;
	int price;
	TextView total, orderid;	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.time);
		
		Spinner spinhours = (Spinner) findViewById(R.id.sp_hours);
		total = (TextView) findViewById(R.id.tv_total);	
		orderid = (TextView) findViewById(R.id.tv_orderid);	
		
		Random order = new Random();
		int randomorder = order.nextInt(9999);		
		order.nextInt(9999);				
		orderid.setText("Order ID: "+randomorder);		
		
		price = 5;					  
		String hours = spinhours.getSelectedItem().toString();
		totalhours = Integer.parseInt(hours);		
		cost = totalhours * price;				
		total.setText("
 
You need to process your spinner when it is changed, not immediately in onCreate. Try this.
Code:
public class Time extends Activity implements OnItemSelectedListener{	
	
	double totalhours, cost;
	int price;
	TextView total, orderid;	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.time);
		
		Spinner spinhours = (Spinner) findViewById(R.id.sp_hours);
		total = (TextView) findViewById(R.id.tv_total);	
		orderid = (TextView) findViewById(R.id.tv_orderid);	
		
		Random order = new Random();
		int randomorder = order.nextInt(9999);		
		order.nextInt(9999);				
		orderid.setText("Order ID: "+randomorder);		
		
		price = 5;					  
	}

        public void onItemSelected(AdapterView<?> parent, View v, int pos, long id)
        {
                String hours = parent.getItemAtPosition(pos).toString();
		totalhours = Integer.parseInt(hours);		
		cost = totalhours * price;				
		total.setText("
 
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 text field take that data and then times it by 2.50 and set it in the text. 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) {
			}
		});		
		
		
		
		
		
	}
	
	

}
 
Back
Top Bottom