Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void saveData() throws InterruptedException {
- SQLiteDatabase db = getBaseContext().openOrCreateDatabase("notes.db", MODE_PRIVATE, null);
- db.execSQL("DROP TABLE IF EXISTS "+TABLE);
- db.execSQL("CREATE TABLE " + TABLE + "( " +
- COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
- COLUMN_NAME + " TEXT," +
- COLUMN_DESCRIPTION + " TEXT," +
- COLUMN_IMPORTANCE + " TEXT," +
- COLUMN_DATE + " TEXT," +
- COLUMN_PICTURE + " INTEGER" +
- ")");
- for (Note n : notes) {
- db.execSQL("INSERT INTO " + TABLE +
- " (" + COLUMN_NAME + ", " + COLUMN_DESCRIPTION + " , " + COLUMN_IMPORTANCE + ", " + COLUMN_DATE + ", " + COLUMN_PICTURE + ")" +
- " VALUES ('"+n.getName()+"' ,'"+n.getDescription()+"', '"+n.getImportance()+"', '"+new SimpleDateFormat("yyyy-MM-dd HH:mm").format(n.getDate())+"', "+n.getPicResource()+");");
- progressBar.setProgress(progressBar.getProgress() + 100/notes.size());
- Thread.sleep(500);
- }
- db.close();
- //Toast.makeText(getApplicationContext(),"data saved", Toast.LENGTH_SHORT).show();
- }
- private ArrayList<Note> loadData() {
- try {
- db = getBaseContext().openOrCreateDatabase("notes.db", MODE_PRIVATE, null);
- db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE + "( " +
- COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
- COLUMN_NAME + " TEXT," +
- COLUMN_DESCRIPTION + " TEXT," +
- COLUMN_IMPORTANCE + " TEXT," +
- COLUMN_DATE + " TEXT," +
- COLUMN_PICTURE + " INTEGER" +
- ")");
- //получаем данные из бд в виде курсора
- noteCursor = db.rawQuery("select * from " + TABLE, null);
- notes = new ArrayList<>();
- if (noteCursor.moveToFirst()) {
- do {
- String name = noteCursor.getString(1);
- String description = noteCursor.getString(2);
- Importance importance = Importance.valueOf(noteCursor.getString(3));
- Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(noteCursor.getString(4));
- int picture = noteCursor.getInt(5);
- notes.add(new Note(name, description, importance, date, picture));
- progressBar.setProgress(progressBar.getProgress() + 100/noteCursor.getCount());
- Thread.sleep(500);
- }
- while (noteCursor.moveToNext());
- }
- }
- catch (ParseException ex) {
- ex.printStackTrace();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- noteCursor.close();
- db.close();
- return notes;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement