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

How to repeat event weekly by using selection on multiple days from my application

我正在使用复选框来选择多天

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 if 这样的条件

  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);

But in calendar there is only first selected day for repetition. How to set all selected day to the repetition days in remindar?

您使用 If-else 检查条件,但这种方法的问题是一旦第一个条件为真,它就会中断并且从不费心去检查其他条件,您的问题的解决方案将迭代在检查复选框的布尔状态之前你需要创建的整个数据集,我宁愿借助 Hashmap 来存储复选框的布尔状态,我会存储周名称并将其与布尔状态配对它并且这会持续一周的所有日子

//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);
        }

像往常一样创造价值

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

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

Iterator it =  dayOfWeek.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry pair = (Map.Entry)it.next();
                System.out.println(pair.getKey() + " = " + pair.getValue());
               if (pair.getValue()==true){
               if (pair.getKey().toString().equalsIgnoreCase("Monday")) {
            values.put(CalendarContract.Reminders.RRULE, "FREQ=WEEKLY;COUNT=1;BYDAY=MO");//UNTIL=1924885800000
        }else   if (pair.getKey().toString().equalsIgnoreCase("Tuesday")) {
            values.put(CalendarContract.Reminders.RRULE, "FREQ=WEEKLY;COUNT=1;BYDAY=TU");
        } else if (pair.getKey().toString().equalsIgnoreCase("Wednesday")) {
            values.put(CalendarContract.Reminders.RRULE, "FREQ=WEEKLY;COUNT=1;BYDAY=WE");
        } else if (pair.getKey().toString().equalsIgnoreCase("Thursday")) {
            values.put(CalendarContract.Reminders.RRULE, "FREQ=WEEKLY;COUNT=1;BYDAY=TH");
        } else if (pair.getKey().toString().equalsIgnoreCase("Friday")) {
            values.put(CalendarContract.Reminders.RRULE, "FREQ=WEEKLY;COUNT=1;BYDAY=FR");
        } else if (pair.getKey().toString().equalsIgnoreCase("Saturday")) {
            values.put(CalendarContract.Reminders.RRULE, "FREQ=WEEKLY;COUNT=1;BYDAY=SA");
        } else if (pair.getKey().toString().equalsIgnoreCase("Sunday")) {
            values.put(CalendarContract.Reminders.RRULE, "FREQ=WEEKLY;COUNT=1;BYDAY=SU");
        }
    }
            }
values.put(CalendarContract.Reminders.DTEND, EndtimeInMilliseconds);

使用这个你将能够遍历整个数据集(在本例中为 Hashmap),为所有被检查的日子插入提醒