Is it possible to add an array or object to SharedPreferences on Android

前端 未结 11 1734
别跟我提以往
别跟我提以往 2020-11-22 11:27

I have an ArrayList of objects that have a name and an icon pointer and I want to save it in SharedPreferences. How can I do?

NOTE:

11条回答
  •  失恋的感觉
    2020-11-22 12:11

    The Simple way is, to convert it to JSON String as below example:

    Gson gson = new Gson();
    String json = gson.toJson(myObj);
    

    Then store the string in the shared preferences. Once you need it just get string from shared preferences and convert back to JSONArray or JSONObject(as per your requirement.)

提交回复
热议问题