0

I've got an ArrayList of a custom object that includes an array. I want to save it to file but when I reload it, the array isn't reloaded. Is there a way to save it?

That's my class:

public class Dates {

        public Date date;
        public String team;
        public String [] players;

        public Dates (Date date, String team, String [] players) {
            this.date = date;
            this.team = team;
            this.players = players;
        }
    }


That's how I create the ArrayList:

public ArrayList<Dates> dates = new ArrayList<>();

How I save and load it:

public static void saveDates(Context context, ArrayList<Dates> callLog) {
        SharedPreferences mPrefs = context.getSharedPreferences("Dates", context.MODE_PRIVATE);
        SharedPreferences.Editor prefsEditor = mPrefs.edit();
        Gson gson = new Gson();
        String json = gson.toJson(callLog);
        prefsEditor.putString("myDates", json);
        prefsEditor.apply();
    }

 public static ArrayList<Dates> loadDates(Context context) {
        ArrayList<Dates> callLog;
        SharedPreferences mPrefs = context.getSharedPreferences("Dates", context.MODE_PRIVATE);
        Gson gson = new Gson();
        String json = mPrefs.getString("myDates", "");
        if (json.isEmpty()) callLog = new ArrayList<>();
        else {
            Type type = new TypeToken<List<Dates>>() {
            }.getType();
            callLog = gson.fromJson(json, type);
        }
        return callLog;
    }


My problem:

String [] player = {"Spieler", "Spieler2", "Spieler3"};
dates.add(new Dates(Calendar.getInstance().getTime(), "F5", player));
saveDates(MainActivity.this, dates);

dates = loadDates(MainActivity.this);

Toast.makeText(MainActivity.this, Integer.toString(dates.get(0).players.length), Toast.LENGTH_SHORT).show();

If I use the first three lines, the length of the array is 3. But if I don't use this three lines, I get a NullPointerException in dates.get(0).players.length because I attempt to get length of null array.

Thanks for your help.

Alex
  • 3
  • 5
  • I think this link can help you: http://stackoverflow.com/questions/5813434/trouble-with-gson-serializing-an-arraylist-of-pojos – Anton Bevza Feb 29 '16 at 10:51
  • *the array isn't reloaded*, are you sure that it does contain something before writing it in the sharedprefence ? – Blackbelt Feb 29 '16 at 10:52
  • You shouldn't use SharedPreferences for data saving, it is meant for user preferences. Here you should use json as mentioned by Anton above, or create a database. For the latest, check the example here http://coderzpassion.com/android-working-with-sqlite-database/ – malrok44 Feb 29 '16 at 11:01
  • Try `prefsEditor.commit()` instead of `prefsEditor.apply()` and check the return value of commit() is true or not. Also as Blackbelt suggested check whether your json string has expected value before committing. – Exception Feb 29 '16 at 11:09
  • I've added an example of my problem – Alex Feb 29 '16 at 13:10
  • @malrok44 Without commenting on whether it's appropriate for the OP's needs, `SharedPreferences` is suitable for storing more than just user preferences. 'Shared preferences are not strictly for saving "user preferences,"' https://developer.android.com/guide/topics/data/data-storage.html#pref – nasch Feb 29 '16 at 19:54

0 Answers0