2017-06-01 23 views
0

我使用複選框多天的選擇如何通過使用多天的選擇,從我的應用程序

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

現在我想通過多次重複天,根據我從選擇天的日曆重複事件週報複選框。對於我,如果情況是這樣

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

但在日曆,只有第一個選擇天重複使用的if..else。如何將所有選定日期設置爲提醒中的重複日期?

+0

我想這會對你有所幫助。 https://stackoverflow.com/questions/44204387/alarmmanager-setinexactrepeating-setwindow-setrepeating-methods-do-not-fire-al/44205413#44205413 –

+0

我希望它使用CalendarContract系統的日曆提醒。 @AniruddhParihar –

+0

你可以再次清楚地解釋你的要求.. !! –

回答

1

您使用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); 

使用這款U將能夠在整個數據集迭代(哈希映射在這種情況下, )插入所有被檢查的日子的提醒

相關問題