private List<Note> createNoteList(Cursor c) { List<Note> l = new ArrayList<Note>(); if (c.moveToFirst()) { do { int id = c.getInt(0); String title = c.getString(1); String text = c.getString(2); Double longitude = c.getDouble(3); Double latitude = c.getDouble(4); String imagePath = c.getString(5); String alarm = c.getString(6); String date = c.getString(7); NoteCategory ncat = NoteCategory.values()[c.getInt(8)]; String address = c.getString(9); int requestCode = c.getInt(10); l.add( new Note( id, title, text, new Location(longitude, latitude), imagePath, alarm, date, ncat, address, requestCode)); } while (c.moveToNext()); } return l; }
/** * @param id Id of the row to retrieve data from * @return a Note object containting all data from the selected row */ public Note getNote(int id) { Cursor c = this.findNoteById(id); c.moveToFirst(); String title = c.getString(1); String text = c.getString(2); Double longitude = c.getDouble(3); Double latitude = c.getDouble(4); String imagePath = c.getString(5); String alarm = c.getString(6); String date = c.getString(7); NoteCategory ncat = NoteCategory.values()[c.getInt(8)]; String address = c.getString(9); int requestCode = c.getInt(10); Note n = new Note( id, title, text, new Location(longitude, latitude), imagePath, alarm, date, ncat, address, requestCode); return n; }
/** * @param id Id of the note to update * @param title Title to update to * @param text Text to update to * @param l Location to update to * @param path ImagePath to update to * @param alarm Alarm to update to * @param ncat Category to update to * @param adr Address to update to * @return true if database was updated, false otherwise */ public boolean updateNote( int id, String title, String text, Location l, String path, String alarm, NoteCategory ncat, String adr) { // Set default values to prevent null in database if (title == null) title = ""; if (text == null) text = ""; if (l == null) l = new Location(0.0, 0.0); if (path == null) path = ""; if (alarm == null) alarm = ""; if (ncat == null) ncat = NoteCategory.NO_CATEGORY; if (adr == null) adr = ""; ContentValues cv = new ContentValues(); cv.put(KEY_TITLE, title); cv.put(KEY_TEXT, text); cv.put(KEY_LONGITUDE, l.getLongitude()); cv.put(KEY_LATITUDE, l.getLatitude()); cv.put(KEY_IMAGEPATH, path); cv.put(KEY_ALARM, alarm); cv.put(KEY_CATEGORY, ncat.ordinal()); cv.put(KEY_ADDRESS, adr); boolean b = this.db.update(TABLE_NOTE, cv, ID + "=" + id, null) > 0; this.setChanged(); this.notifyObservers(DatabaseUpdate.UPDATED_NOTE); return b; }
/** * @param title Title of the note to insert * @param text Text of the note to insert * @param l Location of the note to insert * @param path ImagePath of the note to insert * @param alarm Alarm date of the note to insert * @param ncat Category of the note to insert * @param adr Address of the note to insert * @return Id or -1 if an error occurred */ public long insertNote( String title, String text, Location l, String path, String alarm, NoteCategory ncat, String adr) { // Set default values to prevent null in database if (title == null) title = ""; if (text == null) text = ""; if (l == null) l = new Location(0.0, 0.0); if (path == null) path = ""; if (alarm == null) alarm = ""; if (ncat == null) ncat = NoteCategory.NO_CATEGORY; if (adr == null) adr = ""; ContentValues cv = new ContentValues(); cv.put(KEY_TITLE, title); cv.put(KEY_TEXT, text); cv.put(KEY_LONGITUDE, l.getLongitude()); cv.put(KEY_LATITUDE, l.getLatitude()); cv.put(KEY_IMAGEPATH, path); cv.put(KEY_ALARM, alarm); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(); cv.put(KEY_DATE, dateFormat.format(date)); cv.put(KEY_CATEGORY, ncat.ordinal()); cv.put(KEY_ADDRESS, adr); cv.put(KEY_REQUEST_CODE, -1); long tmp = this.db.insert(TABLE_NOTE, null, cv); this.setChanged(); this.notifyObservers(DatabaseUpdate.NEW_NOTE); return tmp; }
/** * @param id Id of the note which category will be updated * @param ncat The category to update to * @return true if database was updated, false otherwise */ public boolean updateCategory(int id, NoteCategory ncat) { if (ncat == null) ncat = NoteCategory.NO_CATEGORY; ContentValues cv = new ContentValues(); cv.put(KEY_CATEGORY, ncat.ordinal()); boolean b = this.db.update(TABLE_NOTE, cv, ID + "=" + id, null) > 0; this.setChanged(); this.notifyObservers(DatabaseUpdate.UPDATED_NOTE); return b; }