简体   繁体   中英

Adding two Double values in android from two different activities

I'm Making A Project and I got stuck at something, I have 3 activities 1-ItemMenu , 2- ColdDrinks , 3- HotDrinks

There is a Button in ItemMenu Activity To Calculate the total value of items bought from ColdDrinks and HotDrinks, and when I press that button it's supposed to get extras from ColdDrinks and HotDrinks Activities and add the totals of the two screens and display the grand total in a textview in ItemMenu Activity, the problem is that it displays the total of ColdDrinks activity and works works totally fine but it doesn't display the correct total of HotDrinks activity, it worked when I added a new button to get extra from HotDrinks activity but I don't want to have a button for each activity I want just one button to add the total of the ColdDrinks activity and the total of HotDrinks activity and just give me the grand total in the textview

Here is the code of my 3 Activities:

1-ItemMenu Activity :

public class ItemMenu extends AppCompatActivity {

Button cDrinks;
Button hDrinks;
Button sandwiches;
Button snacks;
Button meat;
Button chicken;
Button water;
Button snooker;
Button billiards;
Button qallayat;
Button hookah;
TextView total;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_item_menu);

    cDrinks      =       (  Button  )   findViewById        (  R.id.       cDrinks  )         ;
    hDrinks      =       (  Button  )   findViewById        (  R.id.       hDrinks  )         ;
    sandwiches   =       (  Button  )   findViewById        (  R.id.    sandwiches  )         ;
    snacks       =       (  Button  )   findViewById        (  R.id.        snacks  )         ;
    meat         =       (  Button  )   findViewById        (  R.id.          meat  )         ;
    chicken      =       (  Button  )   findViewById        (  R.id.       chicken  )         ;
    water        =       (  Button  )   findViewById        (  R.id.         water  )         ;
    snooker      =       (  Button  )   findViewById        (  R.id.       snooker  )         ;
    billiards    =       (  Button  )   findViewById        (  R.id.     billiards  )         ;
    qallayat     =       (  Button  )   findViewById        (  R.id.      qallayat  )         ;
    hookah       =       (  Button  )   findViewById        (  R.id.        hookah  )         ;
    total        =       ( TextView )   findViewById        (  R.id.         total  )         ;


    Bundle cdextras = getIntent().getExtras();
    Bundle hdextras = getIntent().getExtras();
    Double imfromcd=cdextras.getDouble("cdtot");
    Double imfromhd=hdextras.getDouble("hdtot");


}

public void cDrinks (View v)
{
    Intent cd= new Intent(getApplicationContext(),ColdDrinks.class);

    startActivity(cd);
}

public void hDrinks (View v)
{

    Intent hd = new Intent(getApplicationContext(),HotDrinks.class);

    startActivity(hd);

}

public void totcalc (View v)
{

    Bundle cdextras = getIntent().getExtras();

    Double imfromcd=cdextras.getDouble("cdtot");

    total.setText(String.valueOf(imfromcd));

}

public void hdtotcalc (View v)
{

    Bundle hdextras = getIntent().getExtras();

    Double imfromhd=hdextras.getDouble("hdtot");

    total.setText(String.valueOf(imfromhd));

}

@Override
public void onBackPressed() {
    finish();
    super.onBackPressed();
}

}

2- ColdDrinks Activity :

public class ColdDrinks extends AppCompatActivity {

Button         cdsave   ;
EditText       epepsi   ;
EditText       eseven   ;
EditText     emiranda   ;
EditText         edew   ;
EditText        ecola   ;
EditText       evimto   ;
EditText    ebarbican   ;
EditText       ezakey   ;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cold_drinks);

    cdsave      =   ( Button )  findViewById    (R.id.     cdsave       )         ;
    epepsi      =   (EditText)  findViewById    (R.id.     epepsi       )         ;
    eseven      =   (EditText)  findViewById    (R.id.     eseven       )         ;
    emiranda    =   (EditText)  findViewById    (R.id.   emiranda       )         ;
    edew        =   (EditText)  findViewById    (R.id.       edew       )         ;
    ecola       =   (EditText)  findViewById    (R.id.      ecola       )         ;
    evimto      =   (EditText)  findViewById    (R.id.     evimto       )         ;
    ebarbican   =   (EditText)  findViewById    (R.id.  ebarbican       )         ;
    ezakey      =   (EditText)  findViewById    (R.id.     ezakey       )         ;




}



public void cdsave (View v)
{

    Double dpepsi = Double.parseDouble(epepsi.getText().toString());
    Double calcpepsi = (dpepsi)*0.30;

    Double dseven = Double.parseDouble(eseven.getText().toString());
    Double calcseven = (dseven)*0.30;

    Double dmiranda = Double.parseDouble(emiranda.getText().toString());
    Double calcmiranda = (dmiranda)*0.30;

    Double ddew = Double.parseDouble(edew.getText().toString());
    Double calcdew = (ddew)*0.30;

    Double dcola = Double.parseDouble(ecola.getText().toString());
    Double calccola = (dcola)*0.30;

    Double dvimto = Double.parseDouble(evimto.getText().toString());
    Double calcvimto = (dvimto)*0.45;

    Double dbarbican = Double.parseDouble(ebarbican.getText().toString());
    Double calcbarbican = (dbarbican)*0.40;

    Double dzakey = Double.parseDouble(ezakey.getText().toString());
    Double calczakey = (dzakey)*0.20;


    Double cdtotal = (calcpepsi)+(calcseven)+(calcmiranda)+(calcdew)+(calccola)+(calcvimto)+(calcbarbican)+(calczakey);

    Intent cdtoim = new Intent(getApplicationContext(),ItemMenu.class);

    cdtoim.putExtra("cdtot",cdtotal);

    startActivity(cdtoim);


}

@Override
public void onBackPressed() {

    Double dpepsi = Double.parseDouble(epepsi.getText().toString());
    Double calcpepsi = (dpepsi)*0.30;

    Double dseven = Double.parseDouble(eseven.getText().toString());
    Double calcseven = (dseven)*0.30;

    Double dmiranda = Double.parseDouble(emiranda.getText().toString());
    Double calcmiranda = (dmiranda)*0.30;

    Double ddew = Double.parseDouble(edew.getText().toString());
    Double calcdew = (ddew)*0.30;

    Double dcola = Double.parseDouble(ecola.getText().toString());
    Double calccola = (dcola)*0.30;

    Double dvimto = Double.parseDouble(evimto.getText().toString());
    Double calcvimto = (dvimto)*0.45;

    Double dbarbican = Double.parseDouble(ebarbican.getText().toString());
    Double calcbarbican = (dbarbican)*0.40;

    Double dzakey = Double.parseDouble(ezakey.getText().toString());
    Double calczakey = (dzakey)*0.20;


    Double cdtotal = (calcpepsi)+(calcseven)+(calcmiranda)+(calcdew)+(calccola)+(calcvimto)+(calcbarbican)+(calczakey);

    Intent cdtoim = new Intent(getApplicationContext(),ItemMenu.class);

    cdtoim.putExtra("cdtot",cdtotal);

    startActivity(cdtoim);

    super.onBackPressed();
}

}

3- HotDrinks Activity :

public class HotDrinks extends AppCompatActivity {

EditText ecoffee;
EditText enescafe;
EditText ecappuccino;
EditText etea;
EditText ezhorat;
EditText eteawmilk;
EditText eteawyansoun;
Button   hdsave;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hot_drinks);

    ecoffee        =    ( EditText )    findViewById    (R.id.             ecoffee      )     ;
    enescafe       =    ( EditText )    findViewById    (R.id.            enescafe      )     ;
    ecappuccino    =    ( EditText )    findViewById    (R.id.         ecappuccino      )     ;
    etea           =    ( EditText )    findViewById    (R.id.                etea      )     ;
    ezhorat        =    ( EditText )    findViewById    (R.id.             ezhorat      )     ;
    eteawmilk      =    ( EditText )    findViewById    (R.id.           eteawmilk      )     ;
    eteawyansoun   =    ( EditText )    findViewById    (R.id.        eteawyansoun      )     ;
    hdsave         =    (  Button  )    findViewById    (R.id.              hdsave      )     ;

}

public void hdsave (View v)
{
    Double dcoffee = Double.parseDouble(ecoffee.getText().toString());
    Double calccoffee = (dcoffee)*0.50;

    Double dnescafe = Double.parseDouble(enescafe.getText().toString());
    Double calcnescafe = (dnescafe)*0.50;

    Double dcappuccino = Double.parseDouble(ecappuccino.getText().toString());
    Double calccappuccino = (dcappuccino)*0.50;

    Double dtea = Double.parseDouble(etea.getText().toString());
    Double calctea = (dtea)*0.25;

    Double dzhorat = Double.parseDouble(ezhorat.getText().toString());
    Double calczhorat = (dzhorat)*0.35;

    Double dteawmilk = Double.parseDouble(eteawmilk.getText().toString());
    Double calcteawmilk = (dteawmilk)*0.40;

    Double dteawyansoun = Double.parseDouble(eteawyansoun.getText().toString());
    Double calcteawyansoun = (dteawyansoun)*0.40;

    Double hdtotal = (calccoffee)+(calcnescafe)+(calccappuccino)+(calctea)+(calczhorat)+(calcteawmilk)+(calcteawyansoun);

    Intent hdtoim = new Intent(getApplicationContext(),ItemMenu.class);

    hdtoim.putExtra("hdtot",hdtotal);

    startActivity(hdtoim);


}

@Override
public void onBackPressed() {

    Double dcoffee = Double.parseDouble(ecoffee.getText().toString());
    Double calccoffee = (dcoffee)*0.50;

    Double dnescafe = Double.parseDouble(enescafe.getText().toString());
    Double calcnescafe = (dnescafe)*0.50;

    Double dcappuccino = Double.parseDouble(ecappuccino.getText().toString());
    Double calccappuccino = (dcappuccino)*0.50;

    Double dtea = Double.parseDouble(etea.getText().toString());
    Double calctea = (dtea)*0.25;

    Double dzhorat = Double.parseDouble(ezhorat.getText().toString());
    Double calczhorat = (dzhorat)*0.35;

    Double dteawmilk = Double.parseDouble(eteawmilk.getText().toString());
    Double calcteawmilk = (dteawmilk)*0.40;

    Double dteawyansoun = Double.parseDouble(eteawyansoun.getText().toString());
    Double calcteawyansoun = (dteawyansoun)*0.40;

    Double hdtotal = (calccoffee)+(calcnescafe)+(calccappuccino)+(calctea)+(calczhorat)+(calcteawmilk)+(calcteawyansoun);

    Intent hdtoim = new Intent(getApplicationContext(),ItemMenu.class);

    hdtoim.putExtra("hdtot",hdtotal);

    startActivity(hdtoim);

    super.onBackPressed();
}

}

You should have a separate class that accumulates the selected items and keeps track of the running total. This class would be a Singleton (only one instance throughout the app), and each Activity reads the total from it and/or submits changes to the selected items.

Your MenuActivity can simply check the current total whenever it resumes.

Have you tried having just one calc function that implements both?

Something along the lines of:

public void totcalc (View v)
{

    Intent intent = getIntent();

    Double imFromHd = intent.getDoubleExtra("hdtot", defaultValue); #defaults are always good
    Double imFromCd = intent.getDoubleExtra("cdtot", defaultValue);
    Double totalCost = imFromHd + imFromCd;

    totalCost += Double.valueOf(total.getText());

    total.setText(total.String.valueOf(totalCost));

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM