From 5d5484077e2282167747ae6ff370d6c49955e536 Mon Sep 17 00:00:00 2001 From: Naiki00 Date: Thu, 11 Feb 2021 22:12:03 +0100 Subject: [PATCH] dodano package bazy danych - zdefiniowano komponenty Room --- .../example/notatkon/database/NoteDao.java | 23 +++++++ .../example/notatkon/database/NoteEntity.java | 62 +++++++++++++++++++ .../notatkon/database/NoteRoomDatabase.java | 35 +++++++++++ .../java/com/example/notatkon/note/Note.java | 29 --------- 4 files changed, 120 insertions(+), 29 deletions(-) create mode 100644 app/src/main/java/com/example/notatkon/database/NoteDao.java create mode 100644 app/src/main/java/com/example/notatkon/database/NoteEntity.java create mode 100644 app/src/main/java/com/example/notatkon/database/NoteRoomDatabase.java delete mode 100644 app/src/main/java/com/example/notatkon/note/Note.java diff --git a/app/src/main/java/com/example/notatkon/database/NoteDao.java b/app/src/main/java/com/example/notatkon/database/NoteDao.java new file mode 100644 index 0000000..0639832 --- /dev/null +++ b/app/src/main/java/com/example/notatkon/database/NoteDao.java @@ -0,0 +1,23 @@ +package com.example.notatkon.database; + +import androidx.lifecycle.LiveData; +import androidx.room.Dao; +import androidx.room.Delete; +import androidx.room.Insert; +import androidx.room.OnConflictStrategy; +import androidx.room.Query; + +import java.util.List; + +@Dao +public interface NoteDao { + + @Query("SELECT * FROM note_table") + LiveData> getNotes(); + + @Insert(onConflict = OnConflictStrategy.IGNORE) + void insert(NoteEntity noteEntity); + + @Delete() + void deleteAll(); +} diff --git a/app/src/main/java/com/example/notatkon/database/NoteEntity.java b/app/src/main/java/com/example/notatkon/database/NoteEntity.java new file mode 100644 index 0000000..949aa20 --- /dev/null +++ b/app/src/main/java/com/example/notatkon/database/NoteEntity.java @@ -0,0 +1,62 @@ +package com.example.notatkon.database; + +import androidx.room.ColumnInfo; +import androidx.room.Entity; +import androidx.room.PrimaryKey; + +import java.util.Random; + +/* +https://developer.android.com/training/data-storage/room/ +https://developer.android.com/codelabs/android-room-with-a-view#4 + +*/ + +@Entity(tableName = "note_table") +public class NoteEntity { + + @PrimaryKey + @ColumnInfo(name = "id") + private int id; + + @ColumnInfo(name = "title") + private String title; + + @ColumnInfo(name = "subheading") + private String subheading; + + @ColumnInfo(name = "content") + private String content; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getSubheading() { + return subheading; + } + + public void setSubheading(String subheading) { + this.subheading = subheading; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/notatkon/database/NoteRoomDatabase.java b/app/src/main/java/com/example/notatkon/database/NoteRoomDatabase.java new file mode 100644 index 0000000..d42e999 --- /dev/null +++ b/app/src/main/java/com/example/notatkon/database/NoteRoomDatabase.java @@ -0,0 +1,35 @@ +package com.example.notatkon.database; + +/* +https://developer.android.com/codelabs/android-room-with-a-view#7 +*/ + +import android.content.Context; + +import androidx.room.Database; +import androidx.room.Room; +import androidx.room.RoomDatabase; + +@Database(entities = {NoteEntity.class}, + version = 1, + exportSchema = false) + +public abstract class NoteRoomDatabase extends RoomDatabase { + + public abstract NoteDao noteDao(); + + private static volatile NoteRoomDatabase INSTANCE; + + static NoteRoomDatabase getNoteRoomDatabase(final Context context) { + if (INSTANCE == null) { + synchronized (NoteRoomDatabase.class) { + if (INSTANCE == null) { + INSTANCE = Room.databaseBuilder(context.getApplicationContext(), + NoteRoomDatabase.class, "note_database") + .build(); + } + } + } + return INSTANCE; + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/notatkon/note/Note.java b/app/src/main/java/com/example/notatkon/note/Note.java deleted file mode 100644 index 574c849..0000000 --- a/app/src/main/java/com/example/notatkon/note/Note.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.example.notatkon.note; - -import java.util.Random; - -public class Note { - private String noteTitle; - private String noteContent; - - // automatyczne dane do tablicy - private static String[] NoteTitles = { "Title1", "Lorem ipsum dolor sit amet, consectetur adipiscing elit" }; - - private static String[] NoteContents = { "Content1", "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\"" }; - - //konstruktor - public Note() { - Random random = new Random(); - - noteTitle = NoteTitles[random.nextInt(NoteTitles.length)]; - noteContent = NoteContents[random.nextInt(NoteContents.length)]; - } - - public String getNoteTitle() { - return noteTitle; - } - - public String getNoteContent() { - return noteContent; - } -} \ No newline at end of file