【问题标题】:Save and load data from sqlite to calendar view from Applandeo library将数据从 sqlite 保存并加载到 Applandeo 库中的日历视图
【发布时间】:2019-12-19 15:58:42
【问题描述】:

我正在使用 applandeo 库中的 Calendarview。我的方式是在日历视图中选择几天,然后将其存储在 SQLite 数据库中并稍后加载。这是我尝试从日历视图中选择几天的方式:

private List<Calendar> getSelectedDays() {
    List<Calendar> calendars = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        Calendar calendar = DateUtils.getCalendar();
        calendar.add(Calendar.DAY_OF_MONTH, i);
        calendars.add(calendar);
    }
    return calendars;
}

这样设置为当天后 10 天。

这就是我尝试在日历视图上显示事件的方式:

Calendar calendar1 = Calendar.getInstance();
    calendar1.add(Calendar.DAY_OF_MONTH, 10);
    events.add(new EventDay(calendar1, R.drawable.sample_icon_2));

如何将选定的日期存储到 sqlite 数据库中然后加载?

从中我可以选择日、月和年并将其存储到数据库中: for (日历: calendarView.getSelectedDates()) { System.out.println(calendar.getTime().toString());

        dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        monthOfYear = calendar.get(Calendar.MONTH);
        year = calendar.get(Calendar.YEAR);

        dbManager.insertWORKING(dayOfMonth,monthOfYear,year);

        System.out.println("DATES: "+dayOfMonth+"/"+monthOfYear+"/"+year);
    }

但是我不知道如何加载它。

【问题讨论】:

    标签: android android-sqlite calendarview


    【解决方案1】:

    您可以将一个方法添加到 dbManager 类中以 query 数据库。

    这样的方法应该是

    public Cursor retrieveAllWorkings() {
        SQliteDatabase db = this.getWritableDatabase();
        return db.query(your_tableName,null,null,null,null,null,null);
    }
    
    • 显然 your_tableName 应该替换为实际的表名。

    • 上面会从表中提取所有行到Cursor

    • 由于第二个参数为空,所有表的列都被提取到游标中。

    然后,您使用适当的 get???? 方法在光标中循环(move using )以从各个列中检索数据。为了灵活性和减少出错的机会,最好不要对列偏移进行硬编码,而是使用 Cursor getColumnIndex 方法从列名中检索列的偏移。

    注意你应该总是close一个光标完成后。

    举个例子:-

    while(the_cursor.moveToNext) {
        String my_extracted_data = the_cursor.getString(the_cursor.getColumnIndex(the_column_name));
        //........ do something with my_extracted_data as it will be overwritten by the next iteration (row in the Cursor)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-11
      • 2020-02-10
      • 2010-09-12
      • 1970-01-01
      • 2012-12-16
      相关资源
      最近更新 更多