0

I am fairly new to android development and i have a dilemma. I want to pass a float from another activity that is in shared preferences and add it to the total.

Basically, what i want to do is:

  1. Get the values from Split Activity and pass it to the Main Activity
  2. total += passed value
  3. display the total
  4. save it in shared preferences

And what i have now:

  1. Get the values from Split Activity and pass it to the Main Activity
  2. it doesn't add in total
  3. Displays only the passed value. not the total

I have tried implementing a float Total = 0; then add the passed valued: Total += passed value But when i display it, it only displays the passed value

All i get is the values are not adding. Which they should. Please enlighten me on this. here are my codes:

Display Bill(Main Activity):

    public void DisplayBill(){

    //Retrive Shared Preferences
    SharedPreferences Billing = getApplicationContext().getSharedPreferences("Bill", MODE_PRIVATE);
    float Total = Billing.getFloat("Bill", 0);

    final TickerView tickerView = (TickerView) findViewById(R.id.ticker_bill);
    tickerView.setCharacterList(TickerUtils.getDefaultNumberList());
    tickerView.setCharacterList(TickerUtils.getDefaultListForUSCurrency());

    tickerView.setText("$".concat(String.valueOf(Total)));
}

Split Bill(Split Activity):

    public void Compute(){

    //SHARED PREFERENCE
    SharedPreferences Billing = getApplicationContext().getSharedPreferences("Bill", MODE_PRIVATE);

    //CES TICKER
    final TickerView tickerCes = (TickerView) findViewById(R.id.tickerCes);
    tickerCes.setCharacterList(TickerUtils.getDefaultNumberList());
    tickerCes.setCharacterList(TickerUtils.getDefaultListForUSCurrency());

    //MON TICKER
    final TickerView tickerMon = (TickerView) findViewById(R.id.tickerMon);
    tickerMon.setCharacterList(TickerUtils.getDefaultNumberList());
    tickerMon.setCharacterList(TickerUtils.getDefaultListForUSCurrency());

    EditText edtBill = (EditText) findViewById(R.id.et_bill);
    float Bill = Float.parseFloat(edtBill.getText().toString());
    float SplitBill;

    SplitBill = (Bill/2);

    tickerCes.setText("$".concat(String.valueOf(SplitBill)));
    tickerMon.setText("$".concat(String.valueOf(SplitBill)));

    //Set Shared Preferences
    SharedPreferences.Editor editor = Billing.edit();
    editor.putFloat("Bill",Bill);
    editor.commit();

}
JJCADIZ
  • 143
  • 2
  • 14

1 Answers1

0

Have you tried this way

This might be the problem.

SharedPreferences.Editor editor = getSharedPreferences("Bill", MODE_PRIVATE).edit();

Here is your full method

      public void Compute(){

            //SHARED PREFERENCE
            //Retrive Shared Preferences
    SharedPreferences Billing = getApplicationContext().getSharedPreferences("Bill", MODE_PRIVATE);


            //CES TICKER
            final TickerView tickerCes = (TickerView) findViewById(R.id.tickerCes);
            tickerCes.setCharacterList(TickerUtils.getDefaultNumberList());
            tickerCes.setCharacterList(TickerUtils.getDefaultListForUSCurrency());

            //MON TICKER
            final TickerView tickerMon = (TickerView) findViewById(R.id.tickerMon);
            tickerMon.setCharacterList(TickerUtils.getDefaultNumberList());
            tickerMon.setCharacterList(TickerUtils.getDefaultListForUSCurrency());

            EditText edtBill = (EditText) findViewById(R.id.et_bill);
            float Bill = Float.parseFloat(edtBill.getText().toString());


            SplitBill = (Bill/2);

 float shared_Total = Billing.getFloat("Bill", 0)+Bill;

            tickerCes.setText("$".concat(String.valueOf(SplitBill)));
            tickerMon.setText("$".concat(String.valueOf(SplitBill)));

            //Set Shared Preferences
            SharedPreferences.Editor editor = getSharedPreferences("Bill", MODE_PRIVATE).edit();
            editor.putFloat("Bill",shared_Total);
            editor.apply();

        }
Arpit Patel
  • 7,212
  • 5
  • 56
  • 67