如何在一周结束时重置Arraylist?

huangapple 未分类评论50阅读模式
英文:

How to RESET Arraylist at the end of the week?

问题

Sure, here's the translation of the provided content:

  1. 我有一个数组列表:

     ArrayList<String> wholeWeekArrayList = new ArrayList<>();
    
  2. 数据库助手(SQLite)将值存储到(上述)数组列表中:

     Cursor data = mDatabaseHelper.getData();
     while (data.moveToNext()) {
         wholeWeekArrayListString.add(data.getString(1));
         // getString(1) 是从SQLite获取的列值
     }
    
  3. 然后我有一个 SumofWeek,其中所有在 wholeWeekArrayListString 中的数据都被相加在一起。
    (我将 wholeWeekArrayListString 转换为 double,以在 TEXTVIEW 中查看它);

  4. 我希望这个 SumOfWeek 在每周结束时重置为零,但是 SQLite 中的数据必须继续累加(以供 SumOfMonth 查看)。
    因此,到周日,数据可能是 $50.00(例如),然后从周一开始。数据必须重置为 0.0,并一直累加到周日为止。
    这必须每周发生一次。

你会如何做到这一点?我尝试了为每个工作日分配一个数字。

Monday = 1;
Tuesday = 2;
Wednesday = 3;
// ...

并且遍历整个星期,但我无法获取(i),它是一周中每个单独日子的数据,然后在周末将其重置,即

int Sunday = 7;

我有总金额,但不知道在周末如何重置数据?

英文:
  1. I have an arraylist:

     ArrayList&lt;String&gt; wholeWeekArrayList = new ArrayList&lt;&gt;();
    
  2. 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
     }
    
  3. Then I have SumofWeek where all the data in wholeWeekArrayListString is added together.
    (I Convert wholeWeekArrayListString to double to view it in a TEXTVIEW);

  4. I want this SumOfWeek to reset to Zero at the end of the week but the data inside the SQLite must keep adding(for SumOfMonth 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&lt;Long&gt; sumofWeek = new ArrayList&lt;&gt;(); 
	private Long resetTimestamp;

	public void populateWeek() {
		if(resetTimestamp == null || isDifferentWeek(resetTimestamp)) {
			sumofWeek = new ArrayList&lt;Long&gt;();
			for(int i = 0; i &lt; 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); 
	}

huangapple
  • 本文由 发表于 2020年5月30日 11:18:18
  • 转载请务必保留本文链接:https://java.coder-hub.com/62097368.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定