Skip to content

Commit f31e9bf

Browse files
committed
mods
1 parent f8c051f commit f31e9bf

File tree

3 files changed

+77
-43
lines changed

3 files changed

+77
-43
lines changed

app/src/main/java/com/spencerstudios/jetdb/MainActivity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
import android.os.Bundle;
44
import android.support.v7.app.AppCompatActivity;
5+
import android.widget.TextView;
56

67
public class MainActivity extends AppCompatActivity {
78

9+
TextView tv;
10+
811
@Override
912
protected void onCreate(Bundle savedInstanceState) {
1013
super.onCreate(savedInstanceState);
1114
setContentView(R.layout.activity_main);
15+
16+
tv = findViewById(R.id.tv);
1217
}
1318
}
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:app="http://schemas.android.com/apk/res-auto"
43
xmlns:tools="http://schemas.android.com/tools"
54
android:layout_width="match_parent"
65
android:layout_height="match_parent"
@@ -11,10 +10,6 @@
1110
android:id="@+id/tv"
1211
android:layout_width="wrap_content"
1312
android:layout_height="wrap_content"
14-
android:text="Hello World!"
15-
app:layout_constraintBottom_toBottomOf="parent"
16-
app:layout_constraintLeft_toLeftOf="parent"
17-
app:layout_constraintRight_toRightOf="parent"
18-
app:layout_constraintTop_toTopOf="parent" />
13+
/>
1914

2015
</android.support.constraint.ConstraintLayout>

jetdblib/src/main/java/spencerstudios/com/jetdblib/JetDB.java

Lines changed: 71 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)