Save ArrayList to SharedPreferences

前端 未结 30 3708
野的像风
野的像风 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条回答
  •  Happy的楠姐
    2020-11-21 05:00

    The best way i have been able to find is a make a 2D Array of keys and put the custom items of the array in the 2-D array of keys and then retrieve it through the 2D arra on startup. I did not like the idea of using string set because most of the android users are still on Gingerbread and using string set requires honeycomb.

    Sample Code: here ditor is the shared pref editor and rowitem is my custom object.

    editor.putString(genrealfeedkey[j][1], Rowitemslist.get(j).getname());
            editor.putString(genrealfeedkey[j][2], Rowitemslist.get(j).getdescription());
            editor.putString(genrealfeedkey[j][3], Rowitemslist.get(j).getlink());
            editor.putString(genrealfeedkey[j][4], Rowitemslist.get(j).getid());
            editor.putString(genrealfeedkey[j][5], Rowitemslist.get(j).getmessage());
    

提交回复
热议问题