0

I am attempting to write an app for my D&D group, and one of the functions will be to store the groups character stats, which i will display in the app.

I went to the following tutorials and documents to try and learn about how to do it, https://codelabs.developers.google.com/codelabs/android-room-with-a-view-kotlin/#0 https://developer.android.com/training/data-storage/room/defining-data.html https://developer.android.com/guide/topics/data/data-storage I got the basics, and created a new kotlin file within my project to define the entity, Dao etc, and will post my code how far i have got, however have reached a dead end of my understanding in trying to get it to work. I am trying to do something as minimal as possible and get that working, and then expand it to store the other character stats.

stats.kt

package com.taylorworld.tw01

import androidx.lifecycle.LiveData
import androidx.room.*

@entity (tablename = "stat_table")
data class Stats(
    @ColumnInfo(name = "stat") val stat: string,
    @PrimaryKey val num: Int)
)

@Dao
interface StatDao {
    @Query("SELECT stat from stat_table")
    fun getStats(): LiveData<<List<Stats>

    @Insert
    suspend fun insert(stat: Stats)
}
@Abstract val statDao: StatDao{}

when i try and compile the project, i get the following error messages. https://imagebin.ca/v/4h9cpZurjoA2

Fábio Nascimento
  • 2,644
  • 1
  • 21
  • 27
philip taylor
  • 17
  • 1
  • 6

2 Answers2

0

I would suggest you to move Entity class and Dao interface to different class.

package com.taylorworld.tw01

import androidx.lifecycle.LiveData
import androidx.room.*
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity (tableName = "stat_table")
data class Stats(
    @ColumnInfo(name = "stat") val stat: String,
    @PrimaryKey val num: Int)

@Dao
interface StatDao {
    @Query("SELECT stat from stat_table")
    fun getStats(): LiveData<List<Stats>>

    @Insert
    suspend fun insert(stat: Stats)

    @Abstract val statDao: StatDao{}
}
John Joe
  • 12,412
  • 16
  • 70
  • 135
0

Are you aware that a RoomDatabase is needed to access the DAOs? It might look something like this:

@Database(
  entities = [(Stats::class)],
  version = 1,
  exportSchema = false
)
abstract class DDDatabase : RoomDatabase() {

  abstract fun statDao(): StatDao

  private var inst: DDDatabase? = null
  private val instLock = Object()

  fun getInstance(context: Context): DDDatabase = inst ?: synchronized(instLock) {
    return inst ?: run {
      inst = Room.databaseBuilder(
        context,
        DDDatabase::class.java, "name of the database"
      ).build()
      inst!!
  }
}

}

however I use the applicationContext instead of injecting a context. You might then later use it as follows:

DDDatabase.getDatabase().statDao().getStats()

I however see that your Query for getStats is not correctly constructed. If you want all of the Stats in the database it should be written as:

@Query("SELECT * from stat_table")
    fun getStats(): LiveData<List<Stats>>

Please mark the answer as approved if it works for you, or get back to me if it doesn't :)

Salladsmannen
  • 113
  • 2
  • 9
  • I added your above posted RoomDatabase into stats.kt, and it resulted in the following errors, https://imagebin.ca/v/4hJeR9Jxm8R1 , i checked it as far as i understand it and it looks right. – philip taylor May 16 '19 at 17:59
  • I see that someone else has had the same issue, I'm not sure what causes it, but you could try the solution stated here: https://stackoverflow.com/questions/44485631/room-persistence-errorentities-and-pojos-must-have-a-usable-public-constructor Now to be a bit clearer, the Database class should be separate from the DAO. The DAO is simply a generated interface which helps generate the classes which will access your database. The class I stated in my answer should exist in addition to your StatsDao, so you should be able to keep your StatsDao and entity as they were prior – Salladsmannen May 17 '19 at 07:52
  • 1
    Get back to me if this is unclear. I also suggest, just as @JohnJoe stated, that you separate the entity and the DAO into two different classes. – Salladsmannen May 17 '19 at 08:00
  • https://github.com/brobostigon/TW01/blob/master/app/src/main/java/com/taylorworld/tw01/stats.kt i separated the entity and DAO as @JohnJoe suggested. https://imagebin.ca/v/4hO8AbCIyPCd i seperated the database as you suggested into something seperate, results in the error shown in that picture. – philip taylor May 17 '19 at 09:22
  • i have made some changes, https://github.com/brobostigon/TW01/commit/13c80e5e9fe3a971f722e50b91a372106dd6fb12 and it certainly gets further and am left with the following error, https://imagebin.ca/v/4hP7SICXFXSr "Expecting a top level declaration". – philip taylor May 17 '19 at 12:41