например, " AndroidStudioProjects\CommunicatorApp\app\build\tmp\kapt3\stubs \debug\com\poststroke\communicatorapp\DictionaryDao.java:21: ошибка: не знаю, как преобразовать курсор в тип возвращаемого значения этого метода (java.lang.Object).
public Abstract java.lang.Object getAllWords( @org.jetbrains.annotations.NotNull"
Я получал ошибку No Dao Entity for несуществующий класс, но перестраивал каждую часть
Вот мой DictionaryDao.kt
Код: Выделить всё
package com.poststroke.communicatorapp
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update
import androidx.room.Delete
import androidx.room.OnConflictStrategy
import com.poststroke.communicatorapp.DictionaryWord
@Dao
interface DictionaryDao {
// Insert a new word into the database
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertWord(word: DictionaryWord)
// Insert multiple words into the database
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertWords(words: List)
/* Query to get all words from the dictionary */
@Query("SELECT * FROM dictionary_words")
suspend fun getAllWords(): List
// Query to get all survival words where 'isSurvivalWord' is true
@Query("SELECT * FROM dictionary_words WHERE isSurvivalWord = 1")
suspend fun getSurvivalWords(): List
// Query to get a specific word by the word value
@Query("SELECT * FROM dictionary_words WHERE word = :word LIMIT 1")
suspend fun getWordByWord(word: String): DictionaryWord?
// Update an existing word in the dictionary
@Update
suspend fun updateWord(word: DictionaryWord): Int
// Delete a word from the dictionary
@Delete
suspend fun deleteWord(word: DictionaryWord): Int
}
Код: Выделить всё
package com.poststroke.communicatorapp
import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
@Database(entities = [DictionaryWord::class], version = 1, exportSchema = false)
abstract class DictionaryDatabase : RoomDatabase() {
abstract fun dictionaryDao(): DictionaryDao
companion object {
@Volatile
private var INSTANCE: DictionaryDatabase? = null
// Get the database instance
fun getDatabase(context: Context): DictionaryDatabase {
// Return the existing database or create a new one, thread-safe
return INSTANCE ?: synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
DictionaryDatabase::class.java,
"dictionary_database" // Database name
).build()
INSTANCE = instance
instance
}
}
}
}
Код: Выделить всё
package com.poststroke.communicatorapp
import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity(tableName = "dictionary_words")
data class DictionaryWord(
@PrimaryKey(autoGenerate = true) val id: Int = 0, // Auto-generated ID for primary key
val word: String, // Word in the dictionary
val category: String, // Category the word belongs to
var isSurvivalWord: Boolean, // Whether it's a survival word
val imagePath: String?, // Path to the image file (nullable)
var frequency: Int = 0 // Frequency count (optional)
)
Код: Выделить всё
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
id("kotlin-kapt")
}
Код: Выделить всё
// Room dependencies
implementation(libs.androidx.room.runtime) // Room Runtime
kapt(libs.androidx.room.compiler) // Room Compiler (Annotation Processing)
implementation(libs.androidx.room.ktx) // Room Kotlin Extensions for coroutine support
}
Код: Выделить всё
[versions]
agp = "8.6.1"
kotlin = "1.9.0"
coreKtx = "1.10.1"
junit = "4.13.2"
junitVersion = "1.1.5"
espressoCore = "3.5.1"
lifecycleRuntimeKtx = "2.6.1"
activityCompose = "1.8.0"
composeBom = "2024.04.01"
appcompat = "1.3.1"
recyclerview = "1.2.1"
material = "1.4.0"
constraintlayout = "2.1.4"
preference = "1.1.1"
room = "2.5.1"
Код: Выделить всё
androidx-room-runtime = { group = "androidx.room", name = "room-runtime", version.ref = "room" }
androidx-room-compiler = { group = "androidx.room", name = "room-compiler", version.ref = "room" }
androidx-room-ktx = { group = "androidx.room", name = "room-ktx", version.ref = "room" }
[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
Подробнее здесь: https://stackoverflow.com/questions/790 ... turn-types