dodano package bazy danych - zdefiniowano komponenty Room

This commit is contained in:
Naiki00 2021-02-11 22:12:03 +01:00
parent 036d0cc9c4
commit 5d5484077e
4 changed files with 120 additions and 29 deletions

View File

@ -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<List<NoteEntity>> getNotes();
@Insert(onConflict = OnConflictStrategy.IGNORE)
void insert(NoteEntity noteEntity);
@Delete()
void deleteAll();
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}