Solution 1 :
If you need the only distinct field you can get List of Strings (just exercises’ names) from your method:
@Query ("SELECT DISTINCT Exercise FROM workout_table WHERE date = :day")
fun getOneExerciseToday(day: String): LiveData<List<String>> // <- List<WorkoutTable> replaced by List<String>
If it’s what you want you should use List in your Repository and ViewModel as well.
Problem :
What I want to do – is retrieve a list of data from one column while ignoring duplicates. I don’t want to delete the duplicates as they signify a new set of each exercise. I have tried using DISTINCT
but I faced an error:
A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
my ROOM table looks like so:
workout_table
id Date Category Exercise weight
---|-----|------------|-------------|---------
1 |Mon | chest | Bench press | 399
2 |Mon | chest | Bench press | 244
5 |Mon | chest | Cross over | 555
3 |Mon | chest | Cross over | 300
4 |Tue | Triceps | Skull Crush | 200
6 |Tue | Shoulders | lat raise | 500
7 |Tue | Shoulders | front raise | 500
its described this way:
@Entity (tableName = "workout_table")
class WorkoutTable(
@PrimaryKey (autoGenerate = true)
val id: Int,
@ColumnInfo(name = "Date")
val date: String,
@ColumnInfo(name = "Category")
val cat: String?,
@ColumnInfo(name = "Exercise")
val exercise: String?,
@ColumnInfo(name = "Weight")
val weight: Double
)
I tried querying it like this: (DAO)
@Query ("SELECT DISTINCT Exercise FROM workout_table WHERE date = :day")
fun getOneExerciseToday(day: String): LiveData<List<WorkoutTable>>
I expect to get: (assuming the day received is Mon
)
- Bench press
- Cross over
and if the day received was Tue
:
- skullcrush
- lat raise
- front raise
Basically, I need only one of each exercise per day, despite there could be duplicates on a day.
Repository:
val getOneExerciseToday : LiveData<List<WorkoutTable>> = wordDao.getOneExerciseToday(todayIS())
ViewModel:
val oneExerciseToday: LiveData<List<WorkoutTable>>
init: oneExerciseToday = repository.getOneExerciseToday
If I remove the distinct attempt and just put it like this:
@Query ("SELECT * FROM workout_table WHERE date = :day")
fun getOneExerciseToday(day: String): LiveData<List<WorkoutTable>>
Everything works, but of course it just prints everything for one day with the duplicates.
Also I’ve tried inspecting the code for an actual error description, but it didn’t give any errors in that result. And the code shows no errors anywhere either. I’ve also tried rebuilding the project, cleaning the code and invalidating the caches and restarting