Saving Serializable Objects List into sharedPreferences

前端 未结 5 1052
闹比i
闹比i 2020-12-03 05:10

Can anyone tell me how I can save a list of custom Serializable objects into SharedPreference? I am new To Android and I want to save an Arr

相关标签:
5条回答
  • 2020-12-03 05:45

    try this, works for me :

    implementation 'com.google.code.gson:gson:2.7'
    

    DataList.java :

    import java.util.List;
    
    public class DataList {
        List<Data> dataList;
    
        public List<Data> getDataList() {
            return dataList;
        }
    
        public void setDataList(List<Data> dataList) {
            this.dataList = dataList;
        }
    }
    

    MainActivity.java :

        List<Data> fetchLog = new ArrayList<Data>();
        DataList dataList = null;
    
    
        dataList = new DataList();
        Gson gson = new Gson();
    
        //SAVE List As SharedPreferences
        Data data = new Data("ali",20);
        fetchLog.add(data);
        dataList.setDataList(fetchLog);
        Gson gson = new Gson();
        String json = gson.toJson(dataList);
        
        utils.setPref("logs", json);// function to set SharedPreferences
    
    
        // read List from SharedPreferences
            String json = utils.getPref("logs");// function to read SharedPreferences
            if (! json.equals("")) {
                dataList = gson.fromJson(json, DataList.class);
                fetchLog = dataList.getDataList();
            }
    
    0 讨论(0)
  • 2020-12-03 05:47

    If it's already serialized, then you can just put it in. the accepted answer in this post will point you in the direction: Save ArrayList to SharedPreferences

    0 讨论(0)
  • 2020-12-03 05:53

    Another good solution is to use GSON. Here's an example:

    private static final String MAP = "map";    
    private static final Type MAP_TYPE = new TypeToken<Map<MyObjA, MyObjB>>() {}.getType();
    
    private static SharedPreferences prefs = MyApplication.getContext().getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    
    private static Map<MyObjA, MyObjB> myMap;
    
    public static void saveMap (Map<MyObjA, MyObjB> map) {
        SharedPreferences.Editor editor = prefs.edit();
    
        editor.putString(MAP, new Gson().toJson(map));
        editor.commit();
        myMap = map;
    }
    
    public static Map<MyObjA, MyObjB> loadMap() {
        if (myMap == null) {
            myMap = new Gson().fromJson(prefs.getString(MAP, null), MAP_TYPE);
        }
        return myMap;
    }
    

    More information about gson at http://code.google.com/p/google-gson/

    Pretty simple right? ;)
    Take care

    0 讨论(0)
  • 2020-12-03 05:56

    You can use the JSON format to serialize your ArrayList and the objects it contains, and then store the String result into the SharedPreferences.

    When you want to get the data back, retrieve the String and use a JSONArray to retrieve each object and add it to a new ArrayList.

    Otherwise you can simply use Object(Input/Output)Stream classes and write it into a differente file using (for writing)

    FileOutputStream fos = this.openFileOutput(fileName, MODE_PRIVATE);
    final OutputStreamWriter osw = new OutputStreamWriter(fos);
    JSONArray array = new JSONArray();
    
    // Add your objects to the array
    
    osw.write(array.toString());
    osw.flush();
    osw.close();
    
    0 讨论(0)
  • 2020-12-03 06:02

    Easiest way out?

    Convert to JSON and save it as a String.

    0 讨论(0)
提交回复
热议问题