Я создаю приложение с базой данных Android. все отлично работает Я могу вставить, обновить и удалить из базы данных. Я пытаюсь сложить столбцы и вернуть их значение в деятельности (UI).
Я попытался добавить запрос в интерфейсе DAO для суммирования столбца, а затем добавил AsyncTask
в репозиторий, но я так растерян, если мне нужно создать новую модель представления и адаптер для итоговой активности
Организация:
@Entity(tableName = "note_table")
public class notetable {
@PrimaryKey(autoGenerate = true)
private int id;
private String date;
private int pay;
private int miles;
private int orders;
private int expenses;
public notetable(String date, int miles, int pay, int orders, int expenses) {
this.date = date;
this.pay = pay;
this.miles = miles;
this.orders = orders;
this.expenses = expenses;
}
}
Интерфейс DAO:
@Dao
public interface notedao {
@Insert
void insert(notetable note);
@Update
void update(notetable note);
@Delete
void delete(notetable note);
@Query("DELETE FROM note_table")
void deleteAllNotes();
@Query("SELECT * FROM note_table ORDER BY date DESC")
LiveData<List<notetable>> getAllNotes();
@Query("SELECT SUM(pay) as totalpay FROM note_table") // this is where
I am stuck
int getPayTotal();
}
The activity in which I am trying to show the totals
public class DashActivity extends AppCompatActivity {
private TextView totaldatecount, totalmiles, totalpay, totalorders,
totalexpenses;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dash);
totaldatecount = (TextView) findViewById(R.id.text_view_totaldays);
totalmiles = (TextView) findViewById(R.id.text_view_totalmiles);
totalpay = (TextView) findViewById(R.id.text_view_totalmoney);
totalorders = (TextView) findViewById(R.id.text_view_totalorder);
totalexpenses = (TextView) findViewById(R.id.text_view_totalexpense);
}
}
Repository:
private static class GetPayTotatlAsyncTask extends AsyncTask<Void, Void,
Void>{
private notedao noteDao;
private GetPayTotatlAsyncTask(notedao noteDao) {
this.noteDao = noteDao;
}
@Override
protected Void doInBackground(Void... voids) {
noteDao.getPayTotal();
return null;
}
}