Reputation: 166
I am using checkbox for selection of multiple days
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);
}
Now i want to pass multiple repetition days to calendar according to my selection of days from checkbox. For that I am using if..else if condition like this
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?
Upvotes: 0
Views: 268
Reputation: 973
Your Checking the Conditions Using If-else,But the problem with this approach is that once the first condition gets true,it breaks out and never bother to check the other conditions,The solution to your issue would be iterating through the entire data set which u need to create before checking boolean states of the checkboxes,I would rather take help of the Hashmap to store the boolean states of the check boxes,i would store the week name and pair up with a boolean state along with it and this continues for all the days of the week
//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);
}
create values as usually
values.put(CalendarContract.Reminders.TITLE, "Routine Custom");
values.put(CalendarContract.Reminders.DTSTART, millisecondsTimesEveryday);
values.put(CalendarContract.Reminders.HAS_ALARM, true);
//Now you need to Iterate through the hashmap to insert reminders for all the checked days:
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);
Using this u would be able to iterate through the entire data set (Hashmap in this case) to insert reminders for all the days which are checked
Upvotes: 1