Room Database DAO Query to SUM() column with int values and return it… Using MVM Model
I am building an app with android room database. everything works fine. I can insert, update and delete from database. I am trying to add up tthe columns and return their value in an activity(UI).
I tried to add a query in the DAO interface to sum the column and then added the AsyncTask
in the repository but I am so confused wether I should make a new viewmodel and adapter for the totals activity
Entity:
@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 Interface:
@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;
}
}
java sqlite android-room
add a comment |
I am building an app with android room database. everything works fine. I can insert, update and delete from database. I am trying to add up tthe columns and return their value in an activity(UI).
I tried to add a query in the DAO interface to sum the column and then added the AsyncTask
in the repository but I am so confused wether I should make a new viewmodel and adapter for the totals activity
Entity:
@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 Interface:
@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;
}
}
java sqlite android-room
Sorry but I did not follow exactly why are you stuck, would you mind elaborate a little more?
– ngueno
Jan 20 at 0:37
I am sorry, I am newbie but trying hard. I have this note_table in a room database. I am using MVM model to populate a recycle view with cardviews from user inputs. Everything works fine. User can insert, update, or delete a note. I am trying to use the SUM() function in the DAO to calculate a column sum. I used this @Query("SELECT SUM(pay) as totalpay FROM note_table") int getPayTotal(); and I used the AsyncTask in the repository with the getPayTotal() but I don't what to do next
– user10909664
Jan 20 at 3:40
Do you want to update your screen with sum?
– Valgaal
Jan 20 at 21:58
Hey Valgaal. yes. I just want to be able to sum those columns(miles,pay...) and display it in my dash activity.
– user10909664
Jan 21 at 0:08
There is no need to make seperate ViewModel/Activity. Could you please explain what exactly is not working in your code?
– ankuranurag2
Jan 23 at 13:21
add a comment |
I am building an app with android room database. everything works fine. I can insert, update and delete from database. I am trying to add up tthe columns and return their value in an activity(UI).
I tried to add a query in the DAO interface to sum the column and then added the AsyncTask
in the repository but I am so confused wether I should make a new viewmodel and adapter for the totals activity
Entity:
@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 Interface:
@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;
}
}
java sqlite android-room
I am building an app with android room database. everything works fine. I can insert, update and delete from database. I am trying to add up tthe columns and return their value in an activity(UI).
I tried to add a query in the DAO interface to sum the column and then added the AsyncTask
in the repository but I am so confused wether I should make a new viewmodel and adapter for the totals activity
Entity:
@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 Interface:
@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;
}
}
java sqlite android-room
java sqlite android-room
edited Jan 20 at 3:49
asked Jan 20 at 0:05
user10909664
Sorry but I did not follow exactly why are you stuck, would you mind elaborate a little more?
– ngueno
Jan 20 at 0:37
I am sorry, I am newbie but trying hard. I have this note_table in a room database. I am using MVM model to populate a recycle view with cardviews from user inputs. Everything works fine. User can insert, update, or delete a note. I am trying to use the SUM() function in the DAO to calculate a column sum. I used this @Query("SELECT SUM(pay) as totalpay FROM note_table") int getPayTotal(); and I used the AsyncTask in the repository with the getPayTotal() but I don't what to do next
– user10909664
Jan 20 at 3:40
Do you want to update your screen with sum?
– Valgaal
Jan 20 at 21:58
Hey Valgaal. yes. I just want to be able to sum those columns(miles,pay...) and display it in my dash activity.
– user10909664
Jan 21 at 0:08
There is no need to make seperate ViewModel/Activity. Could you please explain what exactly is not working in your code?
– ankuranurag2
Jan 23 at 13:21
add a comment |
Sorry but I did not follow exactly why are you stuck, would you mind elaborate a little more?
– ngueno
Jan 20 at 0:37
I am sorry, I am newbie but trying hard. I have this note_table in a room database. I am using MVM model to populate a recycle view with cardviews from user inputs. Everything works fine. User can insert, update, or delete a note. I am trying to use the SUM() function in the DAO to calculate a column sum. I used this @Query("SELECT SUM(pay) as totalpay FROM note_table") int getPayTotal(); and I used the AsyncTask in the repository with the getPayTotal() but I don't what to do next
– user10909664
Jan 20 at 3:40
Do you want to update your screen with sum?
– Valgaal
Jan 20 at 21:58
Hey Valgaal. yes. I just want to be able to sum those columns(miles,pay...) and display it in my dash activity.
– user10909664
Jan 21 at 0:08
There is no need to make seperate ViewModel/Activity. Could you please explain what exactly is not working in your code?
– ankuranurag2
Jan 23 at 13:21
Sorry but I did not follow exactly why are you stuck, would you mind elaborate a little more?
– ngueno
Jan 20 at 0:37
Sorry but I did not follow exactly why are you stuck, would you mind elaborate a little more?
– ngueno
Jan 20 at 0:37
I am sorry, I am newbie but trying hard. I have this note_table in a room database. I am using MVM model to populate a recycle view with cardviews from user inputs. Everything works fine. User can insert, update, or delete a note. I am trying to use the SUM() function in the DAO to calculate a column sum. I used this @Query("SELECT SUM(pay) as totalpay FROM note_table") int getPayTotal(); and I used the AsyncTask in the repository with the getPayTotal() but I don't what to do next
– user10909664
Jan 20 at 3:40
I am sorry, I am newbie but trying hard. I have this note_table in a room database. I am using MVM model to populate a recycle view with cardviews from user inputs. Everything works fine. User can insert, update, or delete a note. I am trying to use the SUM() function in the DAO to calculate a column sum. I used this @Query("SELECT SUM(pay) as totalpay FROM note_table") int getPayTotal(); and I used the AsyncTask in the repository with the getPayTotal() but I don't what to do next
– user10909664
Jan 20 at 3:40
Do you want to update your screen with sum?
– Valgaal
Jan 20 at 21:58
Do you want to update your screen with sum?
– Valgaal
Jan 20 at 21:58
Hey Valgaal. yes. I just want to be able to sum those columns(miles,pay...) and display it in my dash activity.
– user10909664
Jan 21 at 0:08
Hey Valgaal. yes. I just want to be able to sum those columns(miles,pay...) and display it in my dash activity.
– user10909664
Jan 21 at 0:08
There is no need to make seperate ViewModel/Activity. Could you please explain what exactly is not working in your code?
– ankuranurag2
Jan 23 at 13:21
There is no need to make seperate ViewModel/Activity. Could you please explain what exactly is not working in your code?
– ankuranurag2
Jan 23 at 13:21
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54272457%2froom-database-dao-query-to-sum-column-with-int-values-and-return-it-using-m%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54272457%2froom-database-dao-query-to-sum-column-with-int-values-and-return-it-using-m%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Sorry but I did not follow exactly why are you stuck, would you mind elaborate a little more?
– ngueno
Jan 20 at 0:37
I am sorry, I am newbie but trying hard. I have this note_table in a room database. I am using MVM model to populate a recycle view with cardviews from user inputs. Everything works fine. User can insert, update, or delete a note. I am trying to use the SUM() function in the DAO to calculate a column sum. I used this @Query("SELECT SUM(pay) as totalpay FROM note_table") int getPayTotal(); and I used the AsyncTask in the repository with the getPayTotal() but I don't what to do next
– user10909664
Jan 20 at 3:40
Do you want to update your screen with sum?
– Valgaal
Jan 20 at 21:58
Hey Valgaal. yes. I just want to be able to sum those columns(miles,pay...) and display it in my dash activity.
– user10909664
Jan 21 at 0:08
There is no need to make seperate ViewModel/Activity. Could you please explain what exactly is not working in your code?
– ankuranurag2
Jan 23 at 13:21