Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] java – ArrayList of POJOs not saving on all device sharedpreferences

Posted on November 14, 2022

Solution 1 :

I fixed it by calling saveLedger() in onPause(). Not sure why it works though.

Problem :

I’m making a simple budget app, and I have a POJO in my app called budgetLedger, which looks like this:

import java.io.Serializable;

public class budgetDateAndTime implements Serializable {
    int hour, minute, year, month, day;
    double amount;

    public budgetDateAndTime(int hour, int minute, int year, int month, int day, double amount) {
        this.year = year;
        this.month = month;
        this.day = day;
        this.hour = hour;
        this.minute = minute;
        this.amount = amount;
    }

 .....

along with some getters and setters. I made an ArrayList of these and am trying to get them to save in SharedPreferences. The problem is that it saves on my phone (Samsung Galaxy Note 9), but does not seem to save on other phones, and it also doesn’t save in the AndroidStudio emulators. Here are my methods for saving and loading the ArrayList:

public static final String LEDGER_PREFS = "LEDGER_PREF";
public static final String NAME_OF_LED = "ledger";

public void saveLedger() {
    SharedPreferences sharedPref = getActivity().getSharedPreferences(LEDGER_PREFS, getContext().MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    Gson gson = new Gson();
    String json = gson.toJson(budgetLedgerList);
    editor.putString(NAME_OF_LED, json);
    editor.apply();
}

public ArrayList<budgetDateAndTime> getLedger() {
    SharedPreferences sharedPref = getActivity().getSharedPreferences(LEDGER_PREFS, getContext().MODE_PRIVATE);
    Gson gson = new Gson();
    String json = sharedPref.getString(NAME_OF_LED, null);
    Log.d("BTAG", "json in getledger: " + json);
    Type type = new TypeToken<ArrayList<budgetDateAndTime>>() {}.getType();
    ArrayList<budgetDateAndTime> theList = gson.fromJson(json, type);
    return theList;
}

I only call saveLedger() in onDestroy(), and I only assign getLedger() to an ArrayList variable in onCreate(). Again, it works on my phone, but not on any other phones that I have tried. Does anybody know why this is happening? The log shows that the json is empty when the app is closed or phone is restarted, so it’s not a display issue. Any help will be appreciated!

READ  [ANSWERED] android - listview with navigator push to different screens not a single screen
Powered by Inline Related Posts

Comments

Comment posted by naming conventions in Java

OffTopic: Please follow proper

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress