Debora Carnevali
Debora Carnevali

Reputation: 28

Android add day to calendar with for loop

I have an array populated with the id of a database table in ascending order. Now within a list fetch more data to another database table that has a field with the same records in the preceding table. An example

int [] id_nome_op; // for example, contains data (1,2,3)

Now I want to compare this array with data taken from another table (the column for comparison has a field with the same values of the array), then the first match if they are equal, I want to set the date on the calendar today.

When the value changes I want to set the date with TODAY + 1 When the value changes again I want to set the date with TODAY + 2 and so on. I hope I explained myself:

@Override
public List<WkVvent> onMonthChange(int newYear, int newMonth) {
SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd");
    final String strDate = simpleFormat.format(calendarioFooter.getTime());


SQLiteDatabase db = new DatabaseHelper(getActivity()).getReadableDatabase();
        String tabella_op = "SELECT m.id_altra_tabella .....ORDER BY ASC ";
Cursor cur = db.rawQuery(tabella_op, null);
        while (cur.moveToNext()) {
        startTime = (Calendar) calendarioFooter.clone();
        id_altra_tabella = cur.getInt(0);

  for (int i = 0; i < id_nome_op.length; i++) {
            if (id_altra_tabella == id_nome_op[i]) {
                startTime.set(Calendar.DAY_OF_MONTH, 9);
            } else if (id_altra_tabella> id_nome_op[i]) {
                startTime.add(Calendar.DATE, 1);
            }
        }
..
..
..
}

thi is my code, but I do not get the desired result

EDIT: I try to explain: if for example, the array (int [] id_nome_op;) has three values (1,2,3) and the data collected in the list are the same, that is, (1,2,3), with the loop I want to do this : when (id_nome_op) and (id_altra_tabella) are equal to the first value (in this case 1) I have set the calendar with today's date. When this value changes (for example 2) I need to add to the calendar today's date + 1. When changes again (eg 3) I need to add to the calendar today +2, and so on.

Upvotes: 1

Views: 980

Answers (1)

Brian Hibbert
Brian Hibbert

Reputation: 250

I suspect you want the functionality of the .roll() method instead of the .add() method. Roll will adjust the month and year as well as the day. Add will not.

http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html

Edit: My description is backwards...

Upvotes: 1

Related Questions