· 8 years ago · Jul 13, 2018, 10:56 PM
1public class HoursPerDayDataHelper {
2
3private static final String DATABASE_NAME = "database.db";
4private static final int DATABASE_VERSION = 1;
5protected static final String TABLE_NAME = "table";
6protected String TAG = "HoursPerDayDataHelper";
7
8private Context context;
9private SQLiteDatabase db;
10OpenHelper openHelper = null;
11
12public HoursPerDayDataHelper(Context context) {
13 this.context = context;
14 openHelper = new OpenHelper(this.context);
15 this.db = openHelper.getWritableDatabase();
16 openHelper.onCreate(db);
17
18}
19
20public void close() {
21
22 if (openHelper != null) {
23 openHelper.close();
24 }
25
26}
27
28public void deleteAll() {
29 this.db.delete(TABLE_NAME, null, null);
30}
31
32public String selectDuration(String date) {
33
34 String duration = "";
35 Integer value = 0;
36 String returnment = "";
37 Log.i(TAG, "date do select: " + date);
38
39 Cursor cursor = this.db.query(TABLE_NAME, new String[] { "duration" },
40 "date = ? ", new String[]{ date }, null, null, null);
41
42 Log.i(TAG, "cursor string " + cursor);
43
44 if (cursor.moveToFirst()) {
45 do {
46 Log.i(TAG, "dentro do if cursor");
47 duration = cursor.getString(0);
48 value += Integer.parseInt(duration);
49
50 } while (cursor.moveToNext());
51 returnment = Integer.toString(value);
52 }else{
53
54 Log.i(TAG, "bla bla bla");
55
56 }
57
58 if (cursor != null && !cursor.isClosed()) {
59 cursor.close();
60
61 }
62 return returnment;
63}
64
65public ArrayList<String[]> selectTopContacts() {
66
67 ArrayList<String[]> list1 = new ArrayList<String[]>();
68 Cursor cursor = this.db.query(TABLE_NAME, null, null, null, null, null,
69 "duration desc");
70
71 if (cursor.moveToFirst()) {
72 do {
73 if (cursor.getString(2) != "") {
74
75 String[] data = new String[4];
76 data[0] = cursor.getString(2);
77 data[1] = cursor.getString(4);
78 data[2] = cursor.getString(5);
79 data[3] = cursor.getString(7);
80
81 list1.add(data);
82
83 } else {
84
85 String[] data = new String[3];
86 data[1] = cursor.getString(4);
87 data[2] = cursor.getString(5);
88 data[3] = cursor.getString(7);
89
90 list1.add(data);
91
92 }
93 } while (cursor.moveToNext());
94 }
95 if (cursor != null && !cursor.isClosed()) {
96 cursor.close();
97
98 }
99 return list1;
100}
101
102public static class OpenHelper extends SQLiteOpenHelper {
103
104 OpenHelper(Context context) {
105 super(context, DATABASE_NAME, null, DATABASE_VERSION);
106 }
107
108 @Override
109 public void onCreate(SQLiteDatabase db) {
110 db.execSQL("CREATE TABLE IF NOT EXISTS "
111 + TABLE_NAME
112 + "(id INTEGER PRIMARY KEY AUTOINCREMENT, duration TIME, date DATE, current_time TIME)");
113
114 }
115
116 @Override
117 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
118 Log.w("HoursPerDay Database",
119 "Upgrading database, this will drop tables and recreate.");
120 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
121 onCreate(db);
122 }
123}
124
125}
126
127public class HoursPerDay extends Activity{
128
129private String LOG_TAG = "HoursPerDay";
130private TextView mDateDisplay;
131public String date;
132private int mYear;
133private int mMonth;
134private int mDay;
135private int newDay;
136private String hpdData;
137private HoursPerDayDataHelper hpd;
138
139OpenHelper openHelper = new OpenHelper(HoursPerDay.this);
140
141static final int DATE_DIALOG_ID = 0;
142
143protected void onCreate(Bundle savedInstanceState) {
144 super.onCreate(savedInstanceState);
145 setContentView(R.layout.hours_per_day);
146
147 hpd = new HoursPerDayDataHelper(this);
148
149 // capture our View elements
150 mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
151
152 // get the current date
153 final Calendar c = Calendar.getInstance();
154 mYear = c.get(Calendar.YEAR);
155 mMonth = c.get(Calendar.MONTH);
156 mDay = c.get(Calendar.DAY_OF_MONTH);
157 // display the current date (this method is below)
158}
159
160@Override
161protected void onDestroy() {
162 super.onDestroy();
163 if (openHelper != null) {
164 openHelper.close();
165 }
166 if (hpd != null) {
167 hpd.close();
168 }
169}
170
171// the callback received when the user "sets" the date in the dialog
172private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
173
174 public void onDateSet(DatePicker view, int year, int monthOfYear,
175 int dayOfMonth) {
176
177 mYear = year;
178 mMonth = monthOfYear;
179 mDay = dayOfMonth;
180
181 setBasicContent();
182
183 hpd.close();
184
185
186 }
187};
188
189protected Dialog onCreateDialog(int id) {
190 switch (id) {
191 case DATE_DIALOG_ID:
192 return new DatePickerDialog(this,
193 mDateSetListener,
194 mYear, mMonth, mDay);
195 }
196 return null;
197}
198
199@Override
200public boolean onCreateOptionsMenu(Menu menu){
201
202 MenuInflater inflater = getMenuInflater();
203 inflater.inflate(R.layout.hoursperdaymenu, menu);
204 return true;
205
206}
207
208@Override
209public boolean onOptionsItemSelected(MenuItem item){
210
211 switch(item.getItemId()){
212
213 case R.id.filter_by_day:
214 showDialog(DATE_DIALOG_ID);
215 return true;
216 case R.id.filter_by_user:
217
218 showDialog(DATE_DIALOG_ID);
219 return true;
220
221 default:
222 return super.onOptionsItemSelected(item);
223 }
224
225}
226
227public void setBasicContent() {
228
229 date = (mMonth + 1) + "/" + newDay + "/" + mYear;
230 hpdData = this.hpd.selectDuration(date);
231 mDateDisplay.setText(hpdData);
232 hpd.close();
233}
234
235}
236
237public static class OpenHelper extends SQLiteOpenHelper {
238
239 private static final String DATABASE_NAME = "database.db";
240 private static final int DATABASE_VERSION = 1;
241 protected static final String TABLE_NAME = "table";
242 protected String TAG = "HoursPerDayDataHelper";
243
244@Override
245 public void onCreate(SQLiteDatabase db) {
246
247 String query = "CREATE TABLE "
248 + TABLE_NAME
249 + "(id INTEGER PRIMARY KEY AUTOINCREMENT, duration TIME, date DATE, current_time TIME)";
250 db.execSQL(query);
251 }
252 }
253
254@Override
255public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
256
257}
258
259 OpenHelper(Context context) {
260 super(context, DATABASE_NAME, null, DATABASE_VERSION);
261 }
262}
263
264OpenHelper helper = new OpenHelper(ctx);
265// SQLiteDatabase db = helper.getReadableDatabase();
266SQLiteDatabase db = helper.getWritableDatabase();
267
268package ...;
269
270import java.util.ArrayList;
271import java.util.List;
272import android.content.ContentValues;
273import android.content.Context;
274import android.database.Cursor;
275import android.database.sqlite.SQLiteDatabase;
276
277public class DataHandlerDB {
278
279 public static void persistAll(Context ctx, List<Module> moduleList) {
280
281 DatabaseHelper helper = new DatabaseHelper(ctx);
282
283 SQLiteDatabase db = helper.getWritableDatabase();
284
285 ContentValues values = new ContentValues();
286 for (Module m : moduleList) {
287
288 values.put("_id", m.get_id());
289 values.put("name", m.getModule());
290
291 db.insert("module", null, values);
292 }
293 db.close();
294 }
295
296 public static List<Module> findAll(Context ctx) {
297
298 List<Module> result = new ArrayList<Module>();
299 DatabaseHelper helper = new DatabaseHelper(ctx);
300 SQLiteDatabase db = helper.getReadableDatabase();
301
302 Cursor c = db.query(ModuleDB.TABLE_NAME, new String[] { ModuleDB.ID,
303 ModuleDB.MODULE}, null, null, null, null, null);
304
305 while (c.moveToNext()) {
306 Module m = new Module(c.getInt(0), c.getString(1));
307 result.add(m);
308 }
309 c.close();
310 db.close();
311
312 return result;
313 }
314
315 // Update Database entry
316 public static void update(Context ctx, Module m) {
317
318 DatabaseHelper helper = new DatabaseHelper(ctx);
319 SQLiteDatabase db = helper.getWritableDatabase();
320 ContentValues values = new ContentValues();
321
322 values.put("_id", m.get_id());
323 values.put("name", m.getModule());
324
325 db.update("module", values, null, null);
326 db.close();
327 }
328
329 public static void delete(Context ctx, Module m) {
330
331 DatabaseHelper helper = new DatabaseHelper(ctx);
332 SQLiteDatabase db = helper.getWritableDatabase();
333 ContentValues values = new ContentValues();
334
335 values.put("_id", m.get_id());
336 values.put("name", m.getModule());
337
338 db.delete("module","_id = m.get_id()", null);
339 db.close();
340 }
341
342 public static void createDB(Context ctx) {
343 DatabaseHelper helper = new DatabaseHelper(ctx);
344 SQLiteDatabase db = helper.getWritableDatabase();
345 db.close();
346 }
347}
348
349protected void onCreate(Bundle savedInstanceState) {
350 super.onCreate(savedInstanceState);
351
352 // get the a writable DB, in case it's not existing it gets created.
353 DataHandlerDB.createDB(this);
354 // get stuff out of DB
355 moduleList = DataHandlerDB.findAll(this);
356
357 adapter = new ArrayAdapter<Module>(this,
358 android.R.layout.simple_list_item_1, moduleList);
359 setListAdapter(adapter);
360}