英文:
How to RESET Arraylist at the end of the week?
问题
Sure, here's the translation of the provided content:
-
我有一个数组列表:
ArrayList<String> wholeWeekArrayList = new ArrayList<>();
-
数据库助手(SQLite)将值存储到(上述)数组列表中:
Cursor data = mDatabaseHelper.getData(); while (data.moveToNext()) { wholeWeekArrayListString.add(data.getString(1)); // getString(1) 是从SQLite获取的列值 }
-
然后我有一个
SumofWeek
,其中所有在wholeWeekArrayListString
中的数据都被相加在一起。
(我将wholeWeekArrayListString
转换为 double,以在 TEXTVIEW 中查看它); -
我希望这个
SumOfWeek
在每周结束时重置为零,但是 SQLite 中的数据必须继续累加(以供SumOfMonth
查看)。
因此,到周日,数据可能是$50.00
(例如),然后从周一开始。数据必须重置为0.0
,并一直累加到周日为止。
这必须每周发生一次。
你会如何做到这一点?我尝试了为每个工作日分配一个数字。
Monday = 1;
Tuesday = 2;
Wednesday = 3;
// ...
并且遍历整个星期,但我无法获取(i),它是一周中每个单独日子的数据,然后在周末将其重置,即
int Sunday = 7;
我有总金额,但不知道在周末如何重置数据?
英文:
-
I have an arraylist:
ArrayList<String> wholeWeekArrayList = new ArrayList<>();
-
DatabaseHelper(SQLite) is storing the values into the(above) arrayList:
Cursor data = mDatabaseHelper.getData(); while (data.moveToNext() ) { wholeWeekArrayListString.add(data.getString(1)); //getString(1) is column values from SQLite }
-
Then I have
SumofWeek
where all the data inwholeWeekArrayListString
is added together.
(I ConvertwholeWeekArrayListString
to double to view it in a TEXTVIEW); -
I want this
SumOfWeek
to reset to Zero at the end of the week but the data inside the SQLite must keep adding(forSumOfMonth
view).
So by Sunday the data may be $50.00
(for Example) and Starting on int Monday. the data must reset to 0.0
and sum all the way up until Sunday.
This must happen weekly.
How would u do this? I have tried assining days of weeks.
Monday =1;
Tuesday = 2;
Wednesday =3;
// ...
and iterate through the whole week but Im not able to get(i), which is the data of each single day of the week and then reset it on which is
int Sunday =7;
I have the total amount but dont know what method to use to reset the data at the end of the week?
答案1
得分: 0
private ArrayList<Long> sumofWeek = new ArrayList<>();
private Long resetTimestamp;
public void populateWeek() {
if(resetTimestamp == null || isDifferentWeek(resetTimestamp)) {
sumofWeek = new ArrayList<Long>();
for(int i = 0; i < 7; i++){
sumofWeek.add(0l);
}
System.out.println(sumofWeek);
resetTimestamp = java.lang.System.currentTimeMillis();
}
// populate sumofWeek here
}
private boolean isDifferentWeek(long resetTimestamp) {
Calendar cl1 = new GregorianCalendar();
cl1.setTimeInMillis(java.lang.System.currentTimeMillis());
Calendar cl2 = new GregorianCalendar();
cl2.setTimeInMillis(resetTimestamp);
return cl1.get(Calendar.WEEK_OF_YEAR) != cl2.get(Calendar.WEEK_OF_YEAR);
}
英文:
Without changing the DB calls, you´ll have to store the timestamp of the latest reset (resetTimestamp in the example below). If the current time is in a different week, the code will trigger a reset of sumofWeek. After that, it´s your normal sumofWeek logic.
Thread safety: to be added depending on the application specifics.
private ArrayList<Long> sumofWeek = new ArrayList<>();
private Long resetTimestamp;
public void populateWeek() {
if(resetTimestamp == null || isDifferentWeek(resetTimestamp)) {
sumofWeek = new ArrayList<Long>();
for(int i = 0; i < 7; i++){
sumofWeek.add(0l);
}
System.out.println(sumofWeek);
resetTimestamp = java.lang.System.currentTimeMillis();
}
// populate sumofWeek here
}
private boolean isDifferentWeek(long resetTimestamp) {
Calendar cl1 = new GregorianCalendar();
cl1.setTimeInMillis(java.lang.System.currentTimeMillis());
Calendar cl2 = new GregorianCalendar();
cl2.setTimeInMillis(resetTimestamp);
return cl1.get(Calendar.WEEK_OF_YEAR) != cl2.get(Calendar.WEEK_OF_YEAR);
}
专注分享java语言的经验与见解,让所有开发者获益!
评论