0

I have made an app where i'm doing some calculation and storing it in shared preference what i'm trying to do is get that calculated amount and show it in homeactivity where i'm not doing calculation. Just getting the data of shared preference.

What i know is that i can use intent to pass data but how to get value as exactly it changes in mainactivity because in homeactivity it will be shown as just available balance. Here is how i'm doing calculation

 int total = (train_ticket_amount == null || train_ticket_amount.equals("") ? 0:Integer.parseInt(train_ticket_amount))
                + (bus_ticket_amount == null || bus_ticket_amount.equals("")? 0:Integer.parseInt(bus_ticket_amount))
                + (bike_amount == null || bike_amount.equals("")? 0:Integer.parseInt(bike_amount))
                + (share_rickshaw_amount == null || share_rickshaw_amount.equals("")? 0:Integer.parseInt(share_rickshaw_amount));
        result = Integer.parseInt(textavailablebalance2) - total;
        final_amount = textAvailableBalance1.getText().toString();

        if(!expense_amount.isEmpty())
            result+=Integer.parseInt(expense_amount);
        textAvailableBalance1.setText(Integer.toString(result));

        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt("price", result);
        editor.apply();

and calling it in onCreate

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        result = settings.getInt("price",3000);
        textAvailableBalance1.setText(String.valueOf(result));

The calculated amount is getting saved in shared preference in mainactivity but i want the same getting stored in homeactivity without doing calculation in homeactiivty is there a way to do so

Anuraag Baishya
  • 874
  • 2
  • 11
  • 26
  • 1
    English is usually easier to understand when it contains punctuation marks. I have no idea what you're asking. – Dawood ibn Kareem Apr 03 '18 at 09:32
  • sorry for this i was havig problem posting question –  Apr 03 '18 at 09:33
  • are you doing a new calculation in HomeActivity also? – Ranjit Apr 03 '18 at 09:35
  • basically i'm trying to get the calculated data from mainactivity wghich is saved in shared preference to homeactivity as it changes in mainactiivty –  Apr 03 '18 at 09:35
  • @Ranjit no i' not doing calculation in homeactivity –  Apr 03 '18 at 09:36
  • @Ranjit i'm trying to show available balance in homeactivity as it is calculated in mainactivity –  Apr 03 '18 at 09:37

0 Answers0