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) {
}
});
}
}
[/code]
You’ll have to implement a
TextWatcherfor yourEditText. First make an array that will hold the prices of the burgers(mapping the burgers types from the spinner) and anintfield that will hold the current selection from the spinner:then implement the
TextWatcherfor yourEditText:Also in your spinner listener set the currentSelection field:
NOTE: You’ll have to take care about the exception that could arise when parsing the value from the
EditText(for example if the user enters a value and then deletes all the numbers you’ll have an exception because the app will try to parse an empty string)