This is my first question here and I'm beginner in coding so forgive me if my question is not very clear.
I have stored wages in Room database column called "wage" and the datatype is double. There is wages from different days and I would like to show total from those double numbers in my Activity. If tried many different ways to do it, but I just can't get it to work without different errors.
It would be ideal if I could store that sum total into a variable so I can use it easily and probably subtract some value from it if necessary.
I use Repository and ViewModel to access my database and RecyclerView, Adapter and CardView to show different data in my MainActivity. I hope I don't need to use these to access this sum-number but get as direct access to it as possible.
This is my @entity
@Entity (tableName = "shift_table")
public class Shift implements Serializable {
@PrimaryKey(autoGenerate = true)
private int id;
@ColumnInfo(name = "date")
private String date;
@ColumnInfo(name = "start")
private String start;
@ColumnInfo(name = "end")
private String end;
@ColumnInfo (name = "hours")
private String hours;
@ColumnInfo (name = "wage")
private double wage;
public Shift(String date, String start, String end, String hours, double wage) {
this.date = date;
this.start = start;
this.end = end;
this.hours = hours;
this.wage = wage;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public String getDate() {
return date;
}
public String getStart() {
return start;
}
public String getEnd() {
return end;
}
public String getHours() {
return hours;
}
public double getWage() {
return wage;
}
}
This is my @Dao
@Dao
public interface ShiftDao {
@Insert
void insert (Shift shift);
@Update
void update (Shift shift);
@Delete
void delete (Shift shift);
@Query("DELETE FROM shift_table")
void deleteAllShifts();
@Query("SELECT * FROM shift_table ORDER BY id DESC")
LiveData<List<Shift>> getAllShifts();
@Query("SELECT SUM(wage) FROM shift_table")
What here?
}
How I can get that SUM from wage-column to variable and show it in TextView?