Save ArrayList to SharedPreferences

前端 未结 30 3648
野的像风
野的像风 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: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

    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

提交回复
热议问题