I want to get the integer value from my Expense table which is a sum of the expenses(amount column) total expense.
In my Expense Entity class
//This is the column Amount which I want the sum of all values in this column and get single integer value total amount
@ColumnInfo(name = "Amount")
private int amount;
public int getAmount() {
return amount;
}
In my Dao class here is the function which give me total amount through LiveData
@Query("SELECT SUM(Amount) from Expense_table")
LiveData<Integer> getTotalExpenseAmount();
This is from Repository class
public LiveData<Integer> getTotalExpenseAmount() {
return expenseDao.getTotalExpenseAmount();
}
And this is from ViewModel class
public LiveData<Integer> getTotalExpenseAmount() {
return expenseRepository.getTotalExpenseAmount();
}
Everything works fine as it should be. But in my case I don't want to get this through liveData observer, instead I want to get this total amount value itself. Where I don't have to use the observer again and again
expenseViewModel.getTotalExpenseAmount().observe(MainActivity.this, new Observer<Integer>() {
@Override
public void onChanged(Integer integer) {
totalExpense = integer;
addDataSet();
Toast.makeText(MainActivity.this, "Total Expense " + totalExpense, Toast.LENGTH_SHORT).show();
}
});
I've to get this total amount value in many classes where I don't want to observe this again and again. Please tell me any shortcut to do this efficiently. I'll be very thankful to you guys if you solve my problem