Notifications for Custom Calendar in Android -
i have custom calendar application integrated our school kiosk application. calendar must allow adding of events , set notification each event. how can 1 store each event , notify on set time , date? can me? here's calendar code.
public class mycalendar extends activity implements onclicklistener { public calendar month; public calendaradapter adapter; public handler handler; public arraylist<string> items; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); setcontentview(r.layout.calendar); month = calendar.getinstance(); items = new arraylist<string>(); adapter = new calendaradapter(this, month); gridview gridview = (gridview) findviewbyid(r.id.gridview); gridview.setadapter(adapter); handler = new handler(); handler.post(calendarupdater); textview title = (textview) findviewbyid(r.id.title); title.settext(android.text.format.dateformat.format("mmmm yyyy", month)); button previous = (button) findviewbyid(r.id.previous); previous.setonclicklistener(this); button next = (button) findviewbyid(r.id.next); next.setonclicklistener(this); gridview.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> parent, view v, int position, long id) { textview date = (textview) v.findviewbyid(r.id.date); if (date instanceof textview && !date.gettext().equals("")) { intent intent = new intent( "com.gpplsmje.mac.calendar.calendarevents"); intent.putextra( "date", android.text.format.dateformat .format("mmmm", month) + " " + day + ", " + android.text.format.dateformat.format( "yyyy", month)); startactivity(intent); } } }); public void refreshcalendar() { textview title = (textview) findviewbyid(r.id.title); daysadapter.fill(); adapter.refreshdays(); adapter.notifydatasetchanged(); handler.post(calendarupdater); title.settext(android.text.format.dateformat.format("mmmm yyyy", month)); } @override public void onnewintent(intent intent) { string date = intent.getstringextra("date"); string[] datearr = date.split("-"); month.set(integer.parseint(datearr[0]), integer.parseint(datearr[1], integer.parseint(datearr[2]))); } public runnable calendarupdater = new runnable() { @override public void run() { items.clear(); (int = 0; < 31; i++) { random r = new random(); if (r.nextint(10) > 6) { items.add(integer.tostring(i)); } } adapter.setitems(items); adapter.notifydatasetchanged(); } }; @override public void onclick(view v) { switch (v.getid()) { case r.id.previous: if (month.get(calendar.month) == month .getactualminimum(calendar.month)) { month.set((month.get(calendar.year) - 1), month.getactualmaximum(calendar.month), 1); } else { month.set(calendar.month, month.get(calendar.month) - 1); } refreshcalendar(); break; case r.id.next: if (month.get(calendar.month) == month .getactualmaximum(calendar.month)) { month.set((month.get(calendar.year) + 1), month.getactualminimum(calendar.month), 1); } else { month.set(calendar.month, month.get(calendar.month) + 1); } refreshcalendar(); break; } } }
here's calendaradapter class:
public class calendaradapter extends baseadapter { static final int first_day_of_week = 0; private context context; private java.util.calendar month; private calendar selecteddate; private arraylist<string> items; public string[] days; public calendaradapter(context c, calendar monthcalendar) { month = monthcalendar; selecteddate = (calendar) monthcalendar.clone(); context = c; month.set(calendar.day_of_month, 1); this.items = new arraylist<string>(); refreshdays(); } public void setitems(arraylist<string> items) { (int = 0; != items.size(); i++) { if (items.get(i).length() == 1) { items.set(i, "0" + items.get(i)); } } this.items = items; } @override public int getcount() { // todo auto-generated method stub return days.length; } @override public object getitem(int arg0) { // todo auto-generated method stub return null; } @override public long getitemid(int arg0) { // todo auto-generated method stub return 0; } @override public view getview(int position, view convertview, viewgroup parent) { view v = convertview; textview dayview; if (convertview == null) { // if it's not recycled, initialize // attributes layoutinflater vi = (layoutinflater) context .getsystemservice(context.layout_inflater_service); v = vi.inflate(r.layout.calendar_item, null); } dayview = (textview) v.findviewbyid(r.id.date); // disable empty days beginning if (days[position].equals("")) { dayview.setclickable(false); dayview.setfocusable(false); } else { // mark current day focused if (month.get(calendar.year) == selecteddate.get(calendar.year) && month.get(calendar.month) == selecteddate .get(calendar.month) && days[position].equals("" + selecteddate.get(calendar.day_of_month))) { v.setbackgroundresource(r.drawable.item_background_focused); } else { v.setbackgroundresource(r.drawable.calendar_item_bg); } } dayview.settext(days[position]); // create date string comparison string date = days[position]; if (date.length() == 1) { date = "0" + date; } string monthstr = "" + (month.get(calendar.month) + 1); if (monthstr.length() == 1) { monthstr = "0" + monthstr; } // show icon if date not empty , exists in items array imageview iw = (imageview) v.findviewbyid(r.id.date_icon); if (date.length() > 0 && items != null && items.contains(date)) { iw.setvisibility(view.visible); } else { iw.setvisibility(view.invisible); } return v; } public void refreshdays() { // clear items items.clear(); int lastday = month.getactualmaximum(calendar.day_of_month); int firstday = month.get(calendar.day_of_week); int remain = 0; int minus = firstday - 1; if (lastday == 31) { remain = 11 - minus; if (firstday == 1) { days = new string[lastday + (first_day_of_week * 6) + remain]; } else { days = new string[lastday + firstday - (first_day_of_week + 1) + remain]; } } else if (lastday == 30) { remain = 12 - minus; if (firstday == 1) { days = new string[lastday + (first_day_of_week * 6) + remain]; } else { days = new string[lastday + firstday - (first_day_of_week + 1) + remain]; } } else if (lastday == 29) { remain = 13 - minus; if (firstday == 1) { days = new string[lastday + (first_day_of_week * 6) + remain]; } else { days = new string[lastday + firstday - (first_day_of_week + 1) + remain]; } } else if (lastday == 28) { remain = 14 - minus; if (firstday == 1) { days = new string[lastday + (first_day_of_week * 6) + remain]; } else { days = new string[lastday + firstday - (first_day_of_week + 1) + remain]; } } // figure size of array int dummy; if (firstday == 1) { dummy = lastday + (first_day_of_week * 6); } else { dummy = lastday + firstday - (first_day_of_week + 1); } int j = first_day_of_week; // populate empty days before first real day if (firstday > 1) { (j = 0; j < firstday - first_day_of_week; j++) { days[j] = ""; } } else { (j = 0; j < first_day_of_week * 6; j++) { days[j] = ""; } j = first_day_of_week * 6 + 1; // sunday => 1, monday => 7 } // populate days int daynumber = 1; int i; (i = (j - 1); < dummy; i++) { days[i] = "" + daynumber; daynumber++; } for(int k = i; k < days.length; k++){ days[k] = ""; } } }
Comments
Post a Comment