我正在Kotlin写我的第一个Android应用程序。 我想知道如何实例化Room Persistence数据库,以便我可以从许多不同的活动中访问它。 对不起,如果我的问题很愚蠢而且不太具体,但我不确定哪些细节可能有用。
答案 0 :(得分:1)
您可以使用companion object
这样创建实例,
@Database(entities = arrayOf(UserEntity::class), version = 1)
abstract class UserDb : RoomDatabase() {
abstract fun userDao(): UserDao
companion object {
private var INSTANCE: UserDb? = null
fun getInstance(context: Context): UserDb? {
if (INSTANCE == null) {
synchronized(UserDb::class) {
INSTANCE = Room.databaseBuilder(context.applicationContext, UserDb::class.java, "user.db").build()
}
}
return INSTANCE
}
fun destroyInstance() {
INSTANCE = null
}
}
}
您可以在此活动中获取db的实例,
UserDb.getInstance(this)
或者,您也可以使用依赖注入库,如Dagger
答案 1 :(得分:1)
试试这种方式.. 为数据库创建制作如下所示的应用级活动。
class AppActivity:Application() {
companion object {
var db: AppDatabase? = null
fun getDatabase(): AppDatabase? {
return db
}
}
override fun onCreate() {
super.onCreate()
db= Room.databaseBuilder(applicationContext, AppDatabase::class.java,"DB").allowMainThreadQueries().build()
}
}
之后,此活动在应用程序标记中的清单中定义..
android:name=".app.AppActivity"
之后make dao class ..
@Dao
interface TodoDao {
@Query("SELECT * FROM Todo")
fun getTodoData()= mutableListOf<Todo>()
@Insert
fun insertTodo(todo: Todo)
@Update
fun updateTodo(todo: Todo)
@Delete
fun deleteTodo(todo: Todo)
}
make database class for access all dao class ..
@Database(entities = arrayOf(User::class,Assignment::class,Todo::class, Student::class,Event::class,Comment::class,Feedback::class ,Achivement::class,Note::class, Syllabus::class, Education::class, Mark::class, UserWork::class, Exam::class), version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun getUser(): UserDao
abstract fun getStudent(): StudentDao
abstract fun getEducationDao(): EducationDao
abstract fun getWorkDetails(): UserWorkDao
abstract fun getExamDao(): ExamDao
abstract fun getMarkDao(): MarkDao
abstract fun getSyllabusDao(): SyllabusDao
abstract fun getNoteDao(): NoteDao
abstract fun getAchivement():AchivementDao
abstract fun getFeedbackDao():FeedbackDao
abstract fun getCommentDao():CommentDao
abstract fun getEventDao():EventDao
abstract fun getAssignDao():AssignmentDao
abstract fun getTodoDao():TodoDao
}
为待办事项制作表..
@Entity
class Todo {
@PrimaryKey(autoGenerate = true)
var id:Int=0
var time:String=""
var activityName:String=""
var hours:String=""
var types:String=""
}
在活动或片段插入数据后如下...
var data=Todo().apply {
time=mSpTime?.selectedItem.toString()
types=mSpTypes?.selectedItem.toString()
activityName=mEtAname?.text.toString().trim()
hours=mEtHour?.text.toString().trim()
}
AppActivity.getDatabase()?.getTodoDao()?.insertTodo(data)
答案 2 :(得分:0)
查看this link中的文档,在这里您可以找到有关设置Room的详细分步教程。
拥有数据库实例的正确方法是:
AppDatabase db = Room.databaseBuilder(getApplicationContext(),
AppDatabase.class, "database-name").build();
答案 3 :(得分:0)
请记住,您还必须对Kotlin和房间使用一种依赖关系
// Room
implementation "android.arch.persistence.room:runtime:1.1.1"
annotationProcessor "android.arch.persistence.room:compiler:1.1.1"
kapt "android.arch.persistence.room:compiler:1.1.1"
apply plugin: "kotlin-android"
apply plugin: "kotlin-kapt"
apply plugin: "kotlin-android-extensions"