ArrayList中的POJOs未保存在所有设备的sharedpreferences中。

huangapple go评论86阅读模式
英文:

ArrayList of POJOs not saving on all device sharedpreferences

问题

我正在制作一个简单的预算应用程序,我的应用程序中有一个名为budgetLedger的POJO,它看起来像这样:

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;
    }
    
    // 其他方法和getter、setter省略...
}

还有一些getter和setter方法。我创建了一个这些对象的ArrayList,并试图将它们保存在SharedPreferences中。问题在于它可以保存在我的手机上(三星Galaxy Note 9),但似乎无法保存在其他手机上,也无法保存在AndroidStudio模拟器中。以下是我的保存和加载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;
}

我只在onDestroy()中调用saveLedger(),并且只在onCreate()中将getLedger()赋值给一个ArrayList变量。再次强调,在我的手机上可以工作,但在我尝试过的其他手机上无法工作。是否有人知道为什么会发生这种情况?日志显示,当应用程序关闭或手机重新启动时,json为空,所以这不是显示问题。任何帮助将不胜感激!

英文:

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 = &quot;LEDGER_PREF&quot;;
public static final String NAME_OF_LED = &quot;ledger&quot;;

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&lt;budgetDateAndTime&gt; getLedger() {
    SharedPreferences sharedPref = getActivity().getSharedPreferences(LEDGER_PREFS, getContext().MODE_PRIVATE);
    Gson gson = new Gson();
    String json = sharedPref.getString(NAME_OF_LED, null);
    Log.d(&quot;BTAG&quot;, &quot;json in getledger: &quot; + json);
    Type type = new TypeToken&lt;ArrayList&lt;budgetDateAndTime&gt;&gt;() {}.getType();
    ArrayList&lt;budgetDateAndTime&gt; 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!

答案1

得分: 0

我在onPause()中调用了saveLedger()来修复它。不确定为什么它有效。

英文:

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

huangapple
  • 本文由 发表于 2020年8月8日 05:10:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63309184.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定