簡體   English   中英

在Android中通過兩個不同的活動添加兩個Double值

[英]Adding two Double values in android from two different activities

我正在做一個項目,我陷入了困境,我有3個活動:1-ItemMenu,2- ColdDrinks,3- HotDrinks

ItemMenu活動中有一個按鈕,用於計算從ColdDrinks和HotDrinks購買的物品的總價值,當我按下該按鈕時,它應該從ColdDrinks和HotDrinks Activity中獲得額外的東西,並將兩個屏幕的總和相加並顯示總計ItemMenu Activity中的textview,問題是它顯示ColdDrinks活動的總數並且可以正常工作,但是不能正確顯示HotDrinks活動的總數,當我添加新按鈕從HotDrinks活動中獲得額外收益時,它可以工作,但是我不想為每個活動都擁有一個按鈕,我只想一個按鈕來添加ColdDrinks活動的總和和HotDrinks活動的總和,而只需在textview中給我總計

這是我的3個活動的代碼:

1-ItemMenu活動:

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活動:

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活動:

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

}

您應該有一個單獨的類,該類可以累積所選項目並跟蹤運行總計。 此類為Singleton(整個應用程序中只有一個實例),每個Activity從中讀取總計和/或將更改提交到所選項目。

您的MenuActivity只要恢復就可以簡單地檢查當前總數。

您是否嘗試過僅實現兩個函數的calc函數?

類似於以下內容:

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM