如果表的ID为Android,则将日期添加到日历

时间:2015-04-17 16:48:53

标签: android sqlite date calendar

使用此查询我从数据库中获取记录列表,然后根据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;

}

1 个答案:

答案 0 :(得分:0)

在您进行选择时,始终将您的id_operator保存在“共享首选项”中。

检查共享偏好设置中的选择值是否等于您保存的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