如何通过在我的申请中的多天使用选择来每周重复事件

时间:2017-06-01 11:38:28

标签: android calendar reminders

我使用复选框选择多天

if (monday == 1) {
            chkMonday.setChecked(true);

        }  if (tuesday == 1) {
            chkTuesday.setChecked(true);
        }  if (wednesday == 1) {
            chkWedneday.setChecked(true);
        }  if (thursday == 1) {
            chkThursday.setChecked(true);
        }  if (friday == 1) {
            chkFriday.setChecked(true);
        }  if (saturday == 1) {
            chkSaturday.setChecked(true);
        }  if (sunday == 1) {
            chkSunday.setChecked(true);
        }

现在我想根据我从复选框中选择的天数将多个重复日传递给日历。为此我正在使用if..else如果这样的条件

  values.put(CalendarContract.Reminders.TITLE, "Routine Custom");
        values.put(CalendarContract.Reminders.DTSTART, millisecondsTimesEveryday);
        values.put(CalendarContract.Reminders.HAS_ALARM, true);
        if (chkMonday.isChecked()) {
            values.put(CalendarContract.Reminders.RRULE, "FREQ=WEEKLY;COUNT=1;BYDAY=MO");//UNTIL=1924885800000
        }else   if (chkTuesday.isChecked()) {
            values.put(CalendarContract.Reminders.RRULE, "FREQ=WEEKLY;COUNT=1;BYDAY=TU");
        } else if (chkWedneday.isChecked()) {
            values.put(CalendarContract.Reminders.RRULE, "FREQ=WEEKLY;COUNT=1;BYDAY=WE");
        } else if (chkThursday.isChecked()) {
            values.put(CalendarContract.Reminders.RRULE, "FREQ=WEEKLY;COUNT=1;BYDAY=TH");
        } else if (chkFriday.isChecked()) {
            values.put(CalendarContract.Reminders.RRULE, "FREQ=WEEKLY;COUNT=1;BYDAY=FR");
        } else if (chkSaturday.isChecked()) {
            values.put(CalendarContract.Reminders.RRULE, "FREQ=WEEKLY;COUNT=1;BYDAY=SA");
        } else if (chkSunday.isChecked()) {
            values.put(CalendarContract.Reminders.RRULE, "FREQ=WEEKLY;COUNT=1;BYDAY=SU");
        }
        values.put(CalendarContract.Reminders.DTEND, EndtimeInMilliseconds);
  

但是在日历中,只有第一天选择重复日期。如何将所有选定的日期设置为remindar中的重复日期?

1 个答案:

答案 0 :(得分:1)

你使用If-else检查条件,但这种方法的问题是,一旦第一个条件成立,就会爆发并且从不打扰检查其他条件,你的问题的解决方案将遍及整个在检查复选框的布尔状态之前你需要创建的数据集,我宁愿借助Hashmap存储复选框的布尔状态,我会存储周名称并与布尔状态配对,并且这种情况持续一周的所有日子

select      n,
            case 
                when connect_by_isleaf = 1 then 'leaf'
                when level = 1 then 'root'
                else 'inner'
            end 
from        bst
start with  p is null
connect by  p = prior n
order by    n;

通常

创建值
//Create a Hashmap 
 HashMap<String,Boolean> dayOfWeek = new HashMap<>();
if (monday == 1) {
            chkMonday.setChecked(true);
       dayOfWeek.put("Monday",true);

        }  if (tuesday == 1) {
            chkTuesday.setChecked(true);
     dayOfWeek.put("Tuesday",true);//pairing an unique string with a boolean value depending on whether it is checked or not
        }  if (wednesday == 1) {
            chkWedneday.setChecked(true);
        dayOfWeek.put("Wedneday",true);//pairing continues for all days similarly
        }  if (thursday == 1) {
            chkThursday.setChecked(true);
       dayOfWeek.put("Thursday",true);
        }  if (friday == 1) {
            chkFriday.setChecked(true);
       dayOfWeek.put("Friday",true);
        }  if (saturday == 1) {
            chkSaturday.setChecked(true);
       dayOfWeek.put("Saturday",true);
        }  if (sunday == 1) {
            chkSunday.setChecked(true);
       dayOfWeek.put("Sunday",true);
        }

//现在您需要遍历hashmap以插入所有已检查日期的提醒:

values.put(CalendarContract.Reminders.TITLE, "Routine Custom");
        values.put(CalendarContract.Reminders.DTSTART, millisecondsTimesEveryday);
        values.put(CalendarContract.Reminders.HAS_ALARM, true);

使用此功能,您可以遍历整个数据集(在本例中为Hashmap),以便为所有已检查的日期插入提醒