-1

I get JSON from a service, i save it in preference, and when user needs to check session or fetch data, he uses this method getProfileObject(), and it returns ArrayList in return.

This is my method that is responsible to get Json from preference and generate ArrayList by using Gson

SharedPreferences sharedPreferences=ApplicationContext.getAppContext().getSharedPreferences(USER_DATA_PREFERENCE, ApplicationContext.MODE_PRIVATE);

profileObject= sharedPreferences.getString(PROFILE_OBJECT, "");

if(!profileObject.isEmpty()) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    Gson gson = gsonBuilder.create();
    this.dataList = Arrays.asList(gson.fromJson(profileObject, Profile.class));
    return dataList;
}

Now i want to avoid this Gson step each time, I want to store arrayList is shared preference, Is it possible to store java array list in shared preference.

dev90
  • 7,187
  • 15
  • 80
  • 153
  • have u check this https://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences – AskNilesh Jul 03 '18 at 10:30
  • First of all I have doubt on your data structure. Why it is List? And no, you can not do it without GSON. You can consider using database instead it, but again why List? – Pankaj Kumar Jul 03 '18 at 10:33
  • @PankajKumar : its because Gson `Arrays.asList` returns `List` – dev90 Jul 03 '18 at 10:43

3 Answers3

0

Try using View Model class which will save it globally through out the activity life cycle.

hope this helps : https://developer.android.com/topic/libraries/architecture/viewmodel#implement

Anjani Mittal
  • 507
  • 1
  • 7
  • 19
0

Try this way.. Store Array list as serializable object like below ..

editor.putString("key", ObjectSerializer.serialize(currentTasks));// define arrayList

retrive the value.

 currentTasks=(ArrayList<task>)ObjectSerializer.deserialize(prefs.getString(TASKS, ObjectSerializer.serialize(new ArrayList<task>())));



   Gson dataJson = new Gson();
   String userObject = dataJson.toJson(response.body());

after that it store sharedpreference userObject and get data.

  • *`Now i want to avoid this Gson step each time,`* did you read the question? – AskNilesh Jul 03 '18 at 10:38
  • refer this link https://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences –  Jul 03 '18 at 10:41
0
    public static void setArrayListPreference(Context context, ArrayList<String> list, String key){
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        Gson gson = new Gson();
        String json = gson.toJson(list);
        editor.putString(key, json);
        editor.apply();
    }

public static ArrayList<String> getArrayListPreference(Context context, String key){
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        Gson gson = new Gson();
        String json = prefs.getString(key, null);
        Type type = new TypeToken<ArrayList<String>>() {}.getType();
        return gson.fromJson(json, type);
    }
Kamlesh
  • 407
  • 1
  • 3
  • 17