Skip to content
This repository
Browse code

Refactoring and cleaning; preparing for merge

Signed-off-by: Simone <hello@simonewebdesign.it>
  • Loading branch information...
commit a0896c7a03a3a7ad4c030ab784fd60bf94228c78 1 parent f0d18b8
Simone Vittori authored

Showing 1 changed file with 68 additions and 64 deletions. Show diff stats Hide diff stats

  1. 132  src/com/example/todolist/MainActivity.java
132  src/com/example/todolist/MainActivity.java
@@ -24,10 +24,8 @@
24 24
 
25 25
 	private static final String TAG = "MainActivity";
26 26
 	private Context context = null;
27  
-
28 27
 	private ArrayList<ToDoRow> todoRows = null;
29 28
 	private MyAdapter adapter = null;
30  
-
31 29
 	private ListView myListView = null;
32 30
 	private EditText myEditText = null;
33 31
 	private final String FILENAME = "list.json";
@@ -45,11 +43,11 @@ protected void onCreate(Bundle savedInstanceState) {
45 43
 
46 44
 		// Create the array list of to do items
47 45
 		todoRows = new ArrayList<ToDoRow>();
48  
-		
  46
+
49 47
 		// Create mock tasks
50  
-//		todoRows.add(new ToDoRow("Feed the dog", true));
51  
-//		todoRows.add(new ToDoRow("Go to walk", false));
52  
-//		todoRows.add(new ToDoRow("Learn Android development", true));
  48
+		// todoRows.add(new ToDoRow("Feed the dog", true));
  49
+		// todoRows.add(new ToDoRow("Go to walk", false));
  50
+		// todoRows.add(new ToDoRow("Learn Android development", true));
53 51
 
54 52
 		// Create the adapter
55 53
 		adapter = new MyAdapter(this, todoRows);
@@ -63,9 +61,9 @@ protected void onCreate(Bundle savedInstanceState) {
63 61
 		} catch (IOException e) {
64 62
 
65 63
 			e.printStackTrace();
66  
-		}		
  64
+		}
67 65
 		adapter.notifyDataSetChanged();
68  
-		
  66
+
69 67
 		// Bind the event handler to the button
70 68
 		Button b = (Button) findViewById(R.id.myButton);
71 69
 		b.setOnClickListener(buttonListener);
@@ -83,8 +81,9 @@ public void onClick(View v) {
83 81
 
84 82
 			if (Util.isNullOrEmpty(task)) {
85 83
 
86  
-				// task is empty; show toast
  84
+				// task is empty, show toast
87 85
 				Util.showToast(context, "You cannot leave it blank.");
  86
+				
88 87
 			} else {
89 88
 				int index = 0;
90 89
 				todoRows.add(index, new ToDoRow(task));
@@ -105,14 +104,12 @@ protected void onDestroy() {
105 104
 	protected void onPause() {
106 105
 		super.onPause();
107 106
 		Log.v(TAG, "onPause called!");
108  
-		saveData();		
109 107
 	}
110 108
 
111 109
 	@Override
112 110
 	protected void onResume() {
113 111
 		super.onResume();
114 112
 		Log.v(TAG, "onResume called!");
115  
-		saveData();		
116 113
 	}
117 114
 
118 115
 	@Override
@@ -121,101 +118,108 @@ protected void onStop() {
121 118
 		Log.v(TAG, "onStop called!");
122 119
 		saveData();
123 120
 	}
  121
+
124 122
 	
125 123
 	public String getJSON() throws IOException {
126  
-		
127  
-	  JSONArray json = new JSONArray();
128  
-	  
129  
-	  try {
130  
-		  
131  
-		for (ToDoRow row : todoRows) {
132  
-	
133  
-			JSONObject o = new JSONObject();
134  
-			o.put("task", row.getTask());
135  
-			o.put("checked", row.isChecked());
136  
-			json.put(o);
  124
+
  125
+		JSONArray json = new JSONArray();
  126
+
  127
+		try {
  128
+
  129
+			for (ToDoRow row : todoRows) {
  130
+
  131
+				JSONObject o = new JSONObject();
  132
+				o.put("task", row.getTask());
  133
+				o.put("checked", row.isChecked());
  134
+				json.put(o);
  135
+			}
  136
+
  137
+			Log.v(TAG + " writeJSON()", json.toString());
  138
+
  139
+		} catch (JSONException e) {
  140
+			e.printStackTrace();
137 141
 		}
138  
-		
139  
-		Log.v(TAG + " writeJSON()", json.toString());
140  
-		
141  
-	  } catch (JSONException e) {
142  
-		  e.printStackTrace();
143  
-	  }
144  
-	  return json.toString();
  142
+		return json.toString();
145 143
 	}
  144
+
146 145
 	
147 146
 	public void writeJSON() throws IOException {
148  
-		
  147
+
149 148
 		try {
150  
-			
151  
-			FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);					
152  
-			fos.write( getJSON().getBytes() );
  149
+
  150
+			FileOutputStream fos = openFileOutput(FILENAME,
  151
+					Context.MODE_PRIVATE);
  152
+			fos.write(getJSON().getBytes());
153 153
 			fos.close();
154  
-			
  154
+
155 155
 		} catch (IOException e) {
156 156
 
157 157
 			e.printStackTrace();
158 158
 		}
159 159
 	}
  160
+
160 161
 	
161 162
 	public String readJSON() throws FileNotFoundException, IOException {
162  
-		
163  
-		// Constructs a new StringBuffer containing an empty String		
  163
+
  164
+		// Constructs a new StringBuffer containing an empty String
164 165
 		StringBuffer fileContent = new StringBuffer("");
165  
-		
  166
+
166 167
 		try {
167  
-			
  168
+
168 169
 			FileInputStream fis = openFileInput(FILENAME);
169 170
 
170 171
 			byte[] buffer = new byte[1024];
171  
-			/* Reads a single byte from this stream and returns it as an integer 
172  
-			* in the range from 0 to 255. Returns -1 if the end of the stream
173  
-			* has been reached. Blocks until one byte has been read, the end of 
174  
-			* the source stream is detected or an exception is thrown.
175  
-            */
176  
-			while ( fis.read(buffer) != -1 ) {
177  
-			    fileContent.append(new String(buffer));
  172
+			/*
  173
+			 * Reads a single byte from this stream and returns it as an integer
  174
+			 * in the range from 0 to 255. Returns -1 if the end of the stream
  175
+			 * has been reached. Blocks until one byte has been read, the end of
  176
+			 * the source stream is detected or an exception is thrown.
  177
+			 */
  178
+			while (fis.read(buffer) != -1) {
  179
+				fileContent.append(new String(buffer));
178 180
 			}
179  
-			fis.close();			
180  
-			
  181
+			fis.close();
  182
+
181 183
 		} catch (IOException e) {
182 184
 			// could be IOException or FileNotFoundException
183 185
 			e.printStackTrace();
184 186
 		}
185 187
 		return fileContent.toString();
186 188
 	}
  189
+
187 190
 	
188 191
 	public void restoreDataFromJSON(String json) {
189  
-		
  192
+
190 193
 		todoRows.clear();
191  
-		
  194
+
192 195
 		try {
193  
-			
194  
-		  JSONArray data = new JSONArray(json);
195  
-		
196  
-	      for (int i = 0; i < data.length(); i++) {
197  
-		 	  
198  
-		        JSONObject o = data.getJSONObject(i);
199  
-		        
200  
-		        String task = o.getString("task");
201  
-		        boolean checked = o.getBoolean("checked");
202  
-		        
203  
-		        ToDoRow row = new ToDoRow(task, checked);
204  
-		        todoRows.add(row);
205  
-		  }
206  
-			
  196
+
  197
+			JSONArray data = new JSONArray(json);
  198
+
  199
+			for (int i = 0; i < data.length(); i++) {
  200
+
  201
+				JSONObject o = data.getJSONObject(i);
  202
+
  203
+				String task = o.getString("task");
  204
+				boolean checked = o.getBoolean("checked");
  205
+
  206
+				ToDoRow row = new ToDoRow(task, checked);
  207
+				todoRows.add(row);
  208
+			}
  209
+
207 210
 		} catch (JSONException e) {
208 211
 
209 212
 			e.printStackTrace();
210 213
 		}
211 214
 	}
  215
+
212 216
 	
213 217
 	public void saveData() {
214 218
 
215 219
 		try {
216 220
 			writeJSON();
217 221
 		} catch (IOException e) {
218  
-			
  222
+
219 223
 			Log.e(TAG, "JSON writing failed.");
220 224
 			e.printStackTrace();
221 225
 		}

0 notes on commit a0896c7

Please sign in to comment.
Something went wrong with that request. Please try again.