Consider JSON. JSON is well integrated in Android, and you can serialize any complex type of Java object. In your case, the following code is suitable.
// write SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); JSONArray arr = new JSONArray(); arr.put(12); arr.put(-6); prefs.edit().putString("key", arr.toString()); prefs.edit().commit(); // read try { arr = new JSONArray(prefs.getString("key", "[]")); arr.getInt(1); // -6 } catch (JSONException e) { e.printStackTrace(); }
source share