はい、複合オブジェクトを共有設定に保存できます。まあ言ってみれば..
Student mStudentObject = new Student();
SharedPreferences appSharedPrefs = PreferenceManager
.getDefaultSharedPreferences(this.getApplicationContext());
Editor prefsEditor = appSharedPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(mStudentObject);
prefsEditor.putString("MyObject", json);
prefsEditor.commit();
..これで、オブジェクトを次のように取得できます。
SharedPreferences appSharedPrefs = PreferenceManager
.getDefaultSharedPreferences(this.getApplicationContext());
Gson gson = new Gson();
String json = appSharedPrefs.getString("MyObject", "");
Student mStudentObject = gson.fromJson(json, Student.class);
詳細については、ここをクリックしてください。
ArrayList
などの任意のタイプのオブジェクトを取得したい場合はStudent
、次を使用します。
Type type = new TypeToken<List<Student>>(){}.getType();
List<Student> students = gson.fromJson(json, type);