6 Replies - 1082 Views - Last Post: 11 March 2012 - 02:13 PM

#1 brisk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-March 12

Selecting different spinner option doesn't update calculation.

Posted 10 March 2012 - 07:21 AM

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?

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("" + cost);				
		
		
	}


	
}



Is This A Good Question/Topic? 0
  • +

Replies To: Selecting different spinner option doesn't update calculation.

#2 htc(hot-taco-cheese)  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 71
  • Joined: 04-December 10

Re: Selecting different spinner option doesn't update calculation.

Posted 10 March 2012 - 10:50 AM

You need to use an OnItemSelectedListener.

spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 
    @Override 
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
        //use a switch statement on position
    }

    @Override 
    public void onNothingSelected(AdapterView<?> parentView) {
        //do nothing
    }

});



Full example here: http://developer.and...lo-spinner.html
Was This Post Helpful? 1
  • +
  • -

#3 brisk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-March 12

Re: Selecting different spinner option doesn't update calculation.

Posted 10 March 2012 - 10:50 AM

THANK YOU!!!
Was This Post Helpful? 0
  • +
  • -

#4 htc(hot-taco-cheese)  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 71
  • Joined: 04-December 10

Re: Selecting different spinner option doesn't update calculation.

Posted 10 March 2012 - 10:53 AM

Anytime, just reply back if you need any more help
Was This Post Helpful? 0
  • +
  • -

#5 brisk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-March 12

Re: Selecting different spinner option doesn't update calculation.

Posted 11 March 2012 - 09:50 AM

View Posthtc(hot-taco-cheese), on 10 March 2012 - 10:53 AM, said:

Anytime, just reply back if you need any more help


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 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, i'm sure I can figure out the logic etc.


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) {
}
});





}



}
Was This Post Helpful? 0
  • +
  • -

#6 htc(hot-taco-cheese)  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 71
  • Joined: 04-December 10

Re: Selecting different spinner option doesn't update calculation.

Posted 11 March 2012 - 02:11 PM

There are two ways to do that. You could use a Button that updates the TextView every time that it is pressed, or you could use an TextWatcher (TextChangedListener). The latter will work better for you.

Something similar to this could work.

tv = new TextView(this);
et = new EditText(this);
et.setInputType(InputType.TYPE_CLASS_NUMBER); //make sure only numbers can be entered
et.addTextChangedListener(new TextWatcher() {
	@Override
	public void afterTextChanged(Editable s) {
		//get the number from EditText and update the TextView
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count, int after) {
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
	}
});


This post has been edited by htc(hot-taco-cheese): 11 March 2012 - 02:12 PM

Was This Post Helpful? 0
  • +
  • -

#7 brisk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-March 12

Re: Selecting different spinner option doesn't update calculation.

Posted 11 March 2012 - 02:13 PM

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.



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("" + burgerresult);
				} else {
					total.setText("00.00");
				}
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

		
		burgerquantity.addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub

			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
			}

			public void afterTextChanged(Editable s) {
				EditText burgerquantity = (EditText) findViewById(R.id.et_burger);
				TextView total = (TextView) findViewById(R.id.tv_total);
				if (!burgerquantity.getText().toString().equals("")) {

					double result = Integer.parseInt(s.toString())
							* burgerprices[currentSelection]; // obtain the total
					total.setText("" + result); // set the result
				} else {
					total.setText("00.00");	
				}
			}

		});
		

	}

}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1