Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.99 KB | None | 0 0
  1. private void saveData() throws InterruptedException {
  2.  
  3.         SQLiteDatabase db = getBaseContext().openOrCreateDatabase("notes.db", MODE_PRIVATE, null);
  4.         db.execSQL("DROP TABLE IF EXISTS "+TABLE);
  5.         db.execSQL("CREATE TABLE " + TABLE + "( " +
  6.                 COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
  7.                 COLUMN_NAME + " TEXT," +
  8.                 COLUMN_DESCRIPTION + " TEXT," +
  9.                 COLUMN_IMPORTANCE + " TEXT," +
  10.                 COLUMN_DATE + " TEXT," +
  11.                 COLUMN_PICTURE + " INTEGER" +
  12.                 ")");
  13.  
  14.  
  15.         for (Note n : notes) {
  16.             db.execSQL("INSERT INTO " + TABLE +
  17.                     " (" + COLUMN_NAME + ", " + COLUMN_DESCRIPTION + " , " + COLUMN_IMPORTANCE + ", " + COLUMN_DATE + ", " + COLUMN_PICTURE + ")" +
  18.                     " VALUES ('"+n.getName()+"' ,'"+n.getDescription()+"', '"+n.getImportance()+"', '"+new SimpleDateFormat("yyyy-MM-dd HH:mm").format(n.getDate())+"', "+n.getPicResource()+");");
  19.             progressBar.setProgress(progressBar.getProgress() + 100/notes.size());
  20.             Thread.sleep(500);
  21.         }
  22.         db.close();
  23.  
  24.         //Toast.makeText(getApplicationContext(),"data saved", Toast.LENGTH_SHORT).show();
  25.  
  26.  
  27.     }
  28.  
  29.     private ArrayList<Note> loadData() {
  30.  
  31.         try {
  32.             db = getBaseContext().openOrCreateDatabase("notes.db", MODE_PRIVATE, null);
  33.             db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE + "( " +
  34.                     COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
  35.                     COLUMN_NAME + " TEXT," +
  36.                     COLUMN_DESCRIPTION + " TEXT," +
  37.                     COLUMN_IMPORTANCE + " TEXT," +
  38.                     COLUMN_DATE + " TEXT," +
  39.                     COLUMN_PICTURE + " INTEGER" +
  40.                     ")");
  41.  
  42.  
  43.             //получаем данные из бд в виде курсора
  44.             noteCursor = db.rawQuery("select * from " + TABLE, null);
  45.  
  46.             notes = new ArrayList<>();
  47.  
  48.             if (noteCursor.moveToFirst()) {
  49.                 do {
  50.                     String name = noteCursor.getString(1);
  51.                     String description = noteCursor.getString(2);
  52.                     Importance importance = Importance.valueOf(noteCursor.getString(3));
  53.                     Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(noteCursor.getString(4));
  54.                     int picture = noteCursor.getInt(5);
  55.                     notes.add(new Note(name, description, importance, date, picture));
  56.                     progressBar.setProgress(progressBar.getProgress() + 100/noteCursor.getCount());
  57.                     Thread.sleep(500);
  58.                 }
  59.                 while (noteCursor.moveToNext());
  60.             }
  61.         }
  62.         catch (ParseException ex) {
  63.             ex.printStackTrace();
  64.         } catch (InterruptedException e) {
  65.             e.printStackTrace();
  66.         }
  67.         noteCursor.close();
  68.             db.close();
  69.  
  70.  
  71.  
  72.             return notes;
  73.  
  74.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement