I got two array, one is dates and the other one is amount. What I am trying to do is if there is a record with same date, I will increase the amount rather than add another new item into the date.
Here is the code:
for (int i = 0; i < tlist.size(); i++) {
Log.d("Dates", tlist.get(i).getTransactionDate());
Log.d("Amt", String.valueOf(tlist.get(i).getTransactionAmt()));
if(i == tlist.size()-1 || !tlist.get(i).getTransactionDate().equals(tlist.get(i+1).getTransactionDate()))
{
dates[i] = tlist.get(i).getTransactionDate();
totals[i] = tlist.get(i).getTransactionAmt();
}else{
totals[i] += tlist.get(i).getTransactionAmt();
}
}
Sample data:
Dates: 06/05/2017
Amt: 20.0
Dates: 10/05/2017
Amt: 10.8
Dates: 15/05/2017
Amt: 12.0
Dates: 15/05/2017
Amt: 5.0
What I trying to acheive:
Dates: 06/05/2017
Amt: 20.0
Dates: 10/05/2017
Amt: 10.8
Dates: 15/05/2017
Amt: 17.0
When I try to plot it on graph (I am plotting directly using the items in dates and totals array), here is what I get:
Dates: 06/05/2017
Amt: 20.0
Dates: 10/05/2017
Amt: 10.8
Dates: (No label but have amount, this one supposed to be 15/05/2017)
Amt: 12.0
Dates: 15/05/2017
Amt: 0.0
for (int j = 0; j < totals.length; j++) {
// This part plotting the line on the chart
detailSeries.add(j, totals[j]);
// This part setting the value at x-axis
multiRenderer.addXTextLabel(j, dates[j]);
}
Any ideas?
EDIT
I tried to do this but I got index out of bound exception. I guess because I am +1 at the if condition:
if(!tlist.get(i).getTransactionDate().equals(tlist.get(i+1).getTransactionDate())) {
dates[i] = tlist.get(i).getTransactionDate();
amt = tlist.get(i).getTransactionAmt();
if (tlist.size()-2 == i)
{
dates[i] = tlist.get(i+1).getTransactionDate();
amt = tlist.get(i+1).getTransactionAmt();
}
}else{
amt = tlist.get(i).getTransactionAmt() + tlist.get(i+1).getTransactionAmt();
}
totals[i] = amt;