Reputation: 403
The problem is that when I open the fragment the data from from database doesn't appear in Recyclerview and when I debugged I found out that my list size is 0 after opening the fragment
I thought that something wrong with my repository, dao, adapter or activity class I asked on stackoverflow did what I was suggested to but it's still didn't work.
Just if you are curious about previous question ( I did everything in the answer) So guess maybe I've built database and created entities wrong.
Here is how my data structure looks like in JSON format as a example
[
"plant_id":1
"plant_name": 'Benny'
"plant_image":'some url'
"plant_category" : {
"plant_type_id":1
"plant_type_name": 'Aloe'
"plant_type_water" : '1 day per week'
"plant_type_details" : 'poisonous for cats'}
]
So in Sqlite DB Browser I created 2 tables Plant and Plant_Category. Now I porbably think that I might haven't done the Entity class right.
Plant class:
@Entity(tableName = "Plant")
data class Plant(
@PrimaryKey var plant_id:Long,
@ColumnInfo (name="plant_name") var name: String,
@Embedded var type: Plant_Category,
@ColumnInfo(name= "plant_image") var image: Int,
) {
}
Plant_Category Class:
@Entity(tableName = "Plant_Category")
data class Plant_Category(
@PrimaryKey var plant_type_id: Int =0,
@ColumnInfo(name="plant_type_name") var type:String ="",
@ColumnInfo(name="plant_type_water")var water_time: String ="",
@ColumnInfo(name="plant_type_details")var details: String = "",){
}
Database Class:
@Database(entities = [Plant::class, Plant_Category::class], version = 1)
abstract class PlantDatabase:RoomDatabase() {
abstract fun plantDao(): PlantOperations
abstract fun plantCategoryDao(): PlantCategoryOperations
companion object {
private var INSTANCE: PlantDatabase? = null
fun getDatabase(context: Context): PlantDatabase {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(
context.applicationContext,
PlantDatabase::class.java, DB_NAME // directory that leads to database
)
.fallbackToDestructiveMigration()
.build()
}
return INSTANCE!!
}
}
}
I'm very new to the Room Database there is a chance that I've missed something critical. But I will be grateful for your help
Upvotes: 2
Views: 1860
Reputation: 486
First of all it's would be easier to combine elements from 'PlantCategory' and add them to 'Plants' Entity.
Everything is alright with your Database class.Maybe you should add Callback or have your inserted data in your database to test.
Also make sure that you are using database file in your emulator and not in your computer. It's a very common mistake.
Upvotes: 1