Save ArrayList to SharedPreferences

前端 未结 30 3642
野的像风
野的像风 2020-11-21 04:43

I have an ArrayList with custom objects. Each custom object contains a variety of strings and numbers. I need the array to stick around even if the user leaves

相关标签:
30条回答
  • 2020-11-21 05:04

    You can also convert the arraylist into a String and save that in preference

    private String convertToString(ArrayList<String> list) {
    
                StringBuilder sb = new StringBuilder();
                String delim = "";
                for (String s : list)
                {
                    sb.append(delim);
                    sb.append(s);;
                    delim = ",";
                }
                return sb.toString();
            }
    
    private ArrayList<String> convertToArray(String string) {
    
                ArrayList<String> list = new ArrayList<String>(Arrays.asList(string.split(",")));
                return list;
            }
    

    You can save the Arraylist after converting it to string using convertToString method and retrieve the string and convert it to array using convertToArray

    After API 11 you can save set directly to SharedPreferences though !!! :)

    0 讨论(0)
  • 2020-11-21 05:04

    You can convert it to a Map Object to store it, then change the values back to an ArrayList when you retrieve the SharedPreferences.

    0 讨论(0)
  • 2020-11-21 05:04

    It's very simple using getStringSet and putStringSet in SharedPreferences, but in my case, I have to duplicate the Set object before I can add anything to the Set. Or else, the Set will not be saved if my app is force closed. Probably because of the note below in the API below. (It saved though if app is closed by back button).

    Note that you must not modify the set instance returned by this call. The consistency of the stored data is not guaranteed if you do, nor is your ability to modify the instance at all. http://developer.android.com/reference/android/content/SharedPreferences.html#getStringSet

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
    SharedPreferences.Editor editor = prefs.edit();
    
    Set<String> outSet = prefs.getStringSet("key", new HashSet<String>());
    Set<String> workingSet = new HashSet<String>(outSet);
    workingSet.add("Another String");
    
    editor.putStringSet("key", workingSet);
    editor.commit();
    
    0 讨论(0)
  • 2020-11-21 05:05

    Saving Array in SharedPreferences:

    public static boolean saveArray()
    {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor mEdit1 = sp.edit();
        /* sKey is an array */
        mEdit1.putInt("Status_size", sKey.size());  
    
        for(int i=0;i<sKey.size();i++)  
        {
            mEdit1.remove("Status_" + i);
            mEdit1.putString("Status_" + i, sKey.get(i));  
        }
    
        return mEdit1.commit();     
    }
    

    Loading Array Data from SharedPreferences

    public static void loadArray(Context mContext)
    {  
        SharedPreferences mSharedPreference1 =   PreferenceManager.getDefaultSharedPreferences(mContext);
        sKey.clear();
        int size = mSharedPreference1.getInt("Status_size", 0);  
    
        for(int i=0;i<size;i++) 
        {
         sKey.add(mSharedPreference1.getString("Status_" + i, null));
        }
    
    }
    
    0 讨论(0)
  • 2020-11-21 05:06

    You can convert it to JSON String and store the string in the SharedPreferences.

    0 讨论(0)
  • 2020-11-21 05:06

    This is your perfect solution.. try it,

    public void saveArrayList(ArrayList<String> list, String key){
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
        SharedPreferences.Editor editor = prefs.edit();
        Gson gson = new Gson();
        String json = gson.toJson(list);
        editor.putString(key, json);
        editor.apply();     // This line is IMPORTANT !!!
    }
    
    public ArrayList<String> getArrayList(String key){
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
        Gson gson = new Gson();
        String json = prefs.getString(key, null);
        Type type = new TypeToken<ArrayList<String>>() {}.getType();
        return gson.fromJson(json, type);
    }
    
    0 讨论(0)
提交回复
热议问题