@@ -20,16 +20,23 @@ public static void putStringList(Context ctx, ArrayList<String> list, String key
2020 Gson gson = new Gson ();
2121 String json = gson .toJson (list );
2222 editor .putString (key , json );
23- editor .apply (); // This line is IMPORTANT !!!
23+ editor .apply ();
2424 }
2525
2626 public static ArrayList <String > getStringList (Context ctx , String key ) {
27+ ArrayList <String > list ;
2728 SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences (ctx );
2829 Gson gson = new Gson ();
29- String json = prefs .getString (key , null );
30- Type type = new TypeToken <ArrayList <String >>() {
31- }.getType ();
32- return gson .fromJson (json , type );
30+ String json = prefs .getString (key , "" );
31+
32+ if (json .isEmpty ()) {
33+ list = new ArrayList <>();
34+ } else {
35+ Type type = new TypeToken <ArrayList <String >>() {
36+ }.getType ();
37+ list = gson .fromJson (json , type );
38+ }
39+ return list ;
3340 }
3441
3542 //-------------------------- List<Integer> ----------------//
@@ -40,16 +47,23 @@ public static void putIntList(Context ctx, ArrayList<Integer> list, String key)
4047 Gson gson = new Gson ();
4148 String json = gson .toJson (list );
4249 editor .putString (key , json );
43- editor .apply (); // This line is IMPORTANT !!!
50+ editor .apply ();
4451 }
4552
4653 public static ArrayList <Integer > getIntList (Context ctx , String key ) {
54+ ArrayList <Integer > list ;
4755 SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences (ctx );
4856 Gson gson = new Gson ();
49- String json = prefs .getString (key , null );
50- Type type = new TypeToken <ArrayList <Integer >>() {
51- }.getType ();
52- return gson .fromJson (json , type );
57+ String json = prefs .getString (key , "" );
58+
59+ if (json .isEmpty ()){
60+ list = new ArrayList <>();
61+ }else {
62+ Type type = new TypeToken <ArrayList <Integer >>() {
63+ }.getType ();
64+ list = gson .fromJson (json , type );
65+ }
66+ return list ;
5367 }
5468
5569
@@ -61,16 +75,23 @@ public static void putDoubleList(Context ctx, ArrayList<Double> list, String key
6175 Gson gson = new Gson ();
6276 String json = gson .toJson (list );
6377 editor .putString (key , json );
64- editor .apply (); // This line is IMPORTANT !!!
78+ editor .apply ();
6579 }
6680
6781 public static ArrayList <Double > getDoubleList (Context ctx , String key ) {
82+ ArrayList <Double > list ;
6883 SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences (ctx );
6984 Gson gson = new Gson ();
70- String json = prefs .getString (key , null );
71- Type type = new TypeToken <ArrayList <Double >>() {
72- }.getType ();
73- return gson .fromJson (json , type );
85+ String json = prefs .getString (key , "" );
86+
87+ if (json .isEmpty ()){
88+ list = new ArrayList <>();
89+ }else {
90+ Type type = new TypeToken <ArrayList <Double >>() {
91+ }.getType ();
92+ list = gson .fromJson (json , type );
93+ }
94+ return list ;
7495 }
7596
7697
@@ -87,12 +108,19 @@ public static void putFloatList(Context ctx, ArrayList<Float> list, String key)
87108 }
88109
89110 public static ArrayList <Float > getFloatList (Context ctx , String key ) {
111+ ArrayList <Float > list ;
90112 SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences (ctx );
91113 Gson gson = new Gson ();
92- String json = prefs .getString (key , null );
93- Type type = new TypeToken <ArrayList <Float >>() {
94- }.getType ();
95- return gson .fromJson (json , type );
114+ String json = prefs .getString (key , "" );
115+
116+ if (json .isEmpty ()){
117+ list = new ArrayList <>();
118+ }else {
119+ Type type = new TypeToken <ArrayList <Float >>() {
120+ }.getType ();
121+ list = gson .fromJson (json , type );
122+ }
123+ return list ;
96124 }
97125
98126
@@ -109,12 +137,18 @@ public static void putBooleanList(Context ctx, ArrayList<Boolean> list, String k
109137 }
110138
111139 public static ArrayList <Boolean > getBooleanList (Context ctx , String key ) {
140+ ArrayList <Boolean > list ;
112141 SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences (ctx );
113142 Gson gson = new Gson ();
114- String json = prefs .getString (key , null );
115- Type type = new TypeToken <ArrayList <Boolean >>() {
116- }.getType ();
117- return gson .fromJson (json , type );
143+ String json = prefs .getString (key , "" );
144+ if (json .isEmpty ()){
145+ list = new ArrayList <>();
146+ }else {
147+ Type type = new TypeToken <ArrayList <Boolean >>() {
148+ }.getType ();
149+ list = gson .fromJson (json , type );
150+ }
151+ return list ;
118152 }
119153
120154
@@ -145,64 +179,64 @@ public static <T> List<T> getListOfObjects(Context ctx, Class<T> t, String key)
145179
146180 //-----------------------string----------------------------------------//
147181
148- public static String getString (Context ctx , String key ){
182+ public static String getString (Context ctx , String key ) {
149183 SharedPreferences db = PreferenceManager .getDefaultSharedPreferences (ctx );
150184 return db .getString (key , "" );
151185 }
152186
153- public static void putString (Context ctx , String value , String key ){
187+ public static void putString (Context ctx , String value , String key ) {
154188 SharedPreferences db = PreferenceManager .getDefaultSharedPreferences (ctx );
155189 SharedPreferences .Editor editor = db .edit ();
156190 editor .putString (key , value ).apply ();
157191 }
158192
159193 //-----------------------int----------------------------------------//
160194
161- public static int getInt (Context ctx , String key ) {
195+ public static int getInt (Context ctx , String key , int defaultValue ) {
162196 SharedPreferences db = PreferenceManager .getDefaultSharedPreferences (ctx );
163- return db .getInt (key , 0 );
197+ return db .getInt (key , defaultValue );
164198 }
165199
166- public static void putInt (Context ctx , int value , String key ){
200+ public static void putInt (Context ctx , int value , String key ) {
167201 SharedPreferences db = PreferenceManager .getDefaultSharedPreferences (ctx );
168202 SharedPreferences .Editor editor = db .edit ();
169203 editor .putInt (key , value ).apply ();
170204 }
171205
172206 //-----------------------float----------------------------------------//
173207
174- public static double getFloat (Context ctx , String key ) {
208+ public static double getFloat (Context ctx , String key , float defaultValue ) {
175209 SharedPreferences db = PreferenceManager .getDefaultSharedPreferences (ctx );
176- return db .getFloat (key , 0 );
210+ return db .getFloat (key , defaultValue );
177211 }
178212
179- public static void putFloat (Context ctx , float value , String key ){
213+ public static void putFloat (Context ctx , float value , String key ) {
180214 SharedPreferences db = PreferenceManager .getDefaultSharedPreferences (ctx );
181215 SharedPreferences .Editor editor = db .edit ();
182216 editor .putFloat (key , value ).apply ();
183217 }
184218
185219 //-----------------------long----------------------------------------//
186220
187- public static double getLong (Context ctx , String key ) {
221+ public static double getLong (Context ctx , String key , long defaultValue ) {
188222 SharedPreferences db = PreferenceManager .getDefaultSharedPreferences (ctx );
189- return db .getLong (key , 0 );
223+ return db .getLong (key , defaultValue );
190224 }
191225
192- public static void putLong (Context ctx , long value , String key ){
226+ public static void putLong (Context ctx , long value , String key ) {
193227 SharedPreferences db = PreferenceManager .getDefaultSharedPreferences (ctx );
194228 SharedPreferences .Editor editor = db .edit ();
195229 editor .putLong (key , value ).apply ();
196230 }
197231
198232 //-----------------------boolean----------------------------------------//
199233
200- public static boolean getBoolean (Context ctx , String key ) {
234+ public static boolean getBoolean (Context ctx , String key , boolean defaultValue ) {
201235 SharedPreferences db = PreferenceManager .getDefaultSharedPreferences (ctx );
202- return db .getBoolean (key , false );
236+ return db .getBoolean (key , defaultValue );
203237 }
204238
205- public static void putBoolean (Context ctx ,boolean value , String key ){
239+ public static void putBoolean (Context ctx , boolean value , String key ) {
206240 SharedPreferences db = PreferenceManager .getDefaultSharedPreferences (ctx );
207241 SharedPreferences .Editor editor = db .edit ();
208242 editor .putBoolean (key , value ).apply ();
0 commit comments