How to take data from an edit text and times it against a sp

Put your problem here if it does not fit any of the other categories.

How to take data from an edit text and times it against a sp

Postby brisk » Sun Mar 11, 2012 6:00 pm

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

}
brisk
Freshman
Freshman
 
Posts: 6
Joined: Sat Mar 10, 2012 2:44 pm

Top

Re: How to take data from an edit text and times it against

Postby brisk » Sun Mar 11, 2012 11:11 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.



Code: Select all
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");   
            }
         }

      });
      

   }

}

brisk
Freshman
Freshman
 
Posts: 6
Joined: Sat Mar 10, 2012 2:44 pm

Re: How to take data from an edit text and times it against

Postby Phyll » Tue Mar 13, 2012 10:00 pm

Hi brisk,

I think you should only do this once:

EditText burgerquantity = (EditText) findViewById(R.id.et_burger);

Its kind of confusing having it show up in a couple of other places when you already have a global variable. Your app may get confused (or me) and think it is doing something it is not.

I think I would create listeners for anything that can change and if any of them do, recalculate using all the values from all the spinners and any quantity entries you have. Basically one calculating method that is called by any change. You should probably check each EditText for validity before you add it to the total.

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 13 guests