Android 如果 id 为 table,则将日期添加到日历

Android add day to Calendar if id of table

通过此查询,我从数据库中获取了记录列表,并根据 ID 从小到大对它们进行排序。现在查询的id会变的时候,相比之前,我想给日历加一天。我该怎么办?

@Override
public List<WeekViewEvent> onMonthChange(int newYear, int newMonth) {
List<WeekViewEvent> events = new ArrayList<WeekViewEvent>();
SQLiteDatabase db = new DatabaseHelper(getActivity()).getReadableDatabase();
    String tabella_op = "SELECT id_operatore FROM Table ORDER BY id_operatore ASC";
    Cursor cur = db.rawQuery(tabella_op, null);
 while (cur.moveToNext()) {
id_operator = cur.getInt(0);

 Calendar startTime = Calendar.getInstance();
//////////////////////////part of the code for the date change
if(op_confronto == id_operator){
            startTime = Calendar.getInstance();
        }else if(id_operator > op_confronto){
            startTime.add(Calendar.DATE, 1);
            op_confronto = id_operator;
        }
////////////////////////////
...
...
}
    cur.close();
    db.close();
    return events;

}

当您制作 select.

时,请始终在“共享首选项”中保存您的 id_operator

检查 select 值是否等于您在“共享首选项”中保存的 ID,以及是否使用以下代码添加日期。

您有一个使用共享首选项的示例 here

这里有一个关于如何在 java 中向日历添加一天的示例:

String dt = "2008-01-01";  // Start date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(dt));
c.add(Calendar.DATE, 1);  // number of days to add
dt = sdf.format(c.getTime());  // dt is now the new date