android - how to show next week of dates on button click -
please me im using source code http://www.androidhub4you.com/2012/10/custom-calendar-in-android.html below source code customize sample code show current week of dates in horizontal. when user click on next week button nextweek dates show .how show next week of dates on button click? , code not not show date after today see screen shot http://imgur.com/gstbz0x guy me code not show 1 august 2013 on thursday code how current week days in calender show on monday me
public class mycalendaractivity extends activity implements onclicklistener { private static final string tag = "mycalendaractivity"; private textview currentmonth; private button selecteddaymonthyearbutton; private imageview prevmonth; private imageview nextmonth; button btnnextweek,btnpreviousbutton; private gridview calendarview; private gridcelladapter adapter; private calendar _calendar; @suppresslint("newapi") private int month, year; @suppresswarnings("unused") @suppresslint({ "newapi", "newapi", "newapi", "newapi" }) private final dateformat dateformatter = new dateformat(); private static final string datetemplate = "mmmm yyyy"; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.my_calendar_view); _calendar = calendar.getinstance(locale.getdefault()); month = _calendar.get(calendar.month) + 1; year = _calendar.get(calendar.year); selecteddaymonthyearbutton = (button) .findviewbyid(r.id.selecteddaymonthyear); selecteddaymonthyearbutton.settext("selected: "); prevmonth = (imageview) this.findviewbyid(r.id.prevmonth); prevmonth.setonclicklistener(this); currentmonth = (textview) this.findviewbyid(r.id.currentmonth); currentmonth.settext(dateformat.format(datetemplate, _calendar.gettime())); nextmonth = (imageview) this.findviewbyid(r.id.nextmonth); nextmonth.setonclicklistener(this); btnnextweek = (button) findviewbyid(r.id.btnnextweek); btnnextweek.setonclicklistener(new onclicklistener() { public void onclick(view arg0) { // todo auto-generated method stub //code show next week dates } }); btnpreviousbutton = (button) findviewbyid(r.id.btnpreviousbutton); btnpreviousbutton.setonclicklistener(new onclicklistener() { public void onclick(view arg0) { // todo auto-generated method stub //code show previous week dates } }); calendarview = (gridview) this.findviewbyid(r.id.calendar); adapter = new gridcelladapter(getapplicationcontext(), r.id.calendar_day_gridcell, month, year); adapter.notifydatasetchanged(); calendarview.setadapter(adapter); } /** * * @param month * @param year */ private void setgridcelladaptertodate(int month, int year) { adapter = new gridcelladapter(getapplicationcontext(), r.id.calendar_day_gridcell, month, year); _calendar.set(year, month - 1, _calendar.get(calendar.day_of_week)); currentmonth.settext(dateformat.format(datetemplate, _calendar.gettime())); adapter.notifydatasetchanged(); calendarview.setadapter(adapter); } @override public void onclick(view v) { if (v == prevmonth) { if (month <= 1) { month = 12; year--; } else { month--; } log.d(tag, "setting prev month in gridcelladapter: " + "month: " + month + " year: " + year); setgridcelladaptertodate(month, year); } if (v == nextmonth) { if (month > 11) { month = 1; year++; } else { month++; } log.d(tag, "setting next month in gridcelladapter: " + "month: " + month + " year: " + year); setgridcelladaptertodate(month, year); } } @override public void ondestroy() { log.d(tag, "destroying view ..."); super.ondestroy(); } // inner class public class gridcelladapter extends baseadapter implements onclicklistener { private static final string tag = "gridcelladapter"; private final context _context; private final list<string> list; private static final int day_offset = 1; private final string[] weekdays = new string[] { "sun", "mon", "tue", "wed", "thu", "fri", "sat" }; private final string[] months = { "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december" }; private final int[] daysofmonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; private int daysinmonth; private int currentdayofmonth; private int currentweekday; private button gridcell; private textview num_events_per_day; private final hashmap<string, integer> eventspermonthmap; private final simpledateformat dateformatter = new simpledateformat( "dd-mmm-yyyy"); // days in current month public gridcelladapter(context context, int textviewresourceid, int month, int year) { super(); this._context = context; this.list = new arraylist<string>(); log.d(tag, "==> passed in date month: " + month + " " + "year: " + year); calendar calendar = calendar.getinstance(); setcurrentdayofmonth(calendar.get(calendar.day_of_month)); setcurrentweekday(calendar.get(calendar.day_of_week)); log.d(tag, "new calendar:= " + calendar.gettime().tostring()); log.d(tag, "currentdayofweek :" + getcurrentweekday()); log.d(tag, "currentdayofmonth :" + getcurrentdayofmonth()); // print month printmonth(month, year); // find number of events eventspermonthmap = findnumberofeventspermonth(year, month); } private string getmonthasstring(int i) { return months[i]; } private string getweekdayasstring(int i) { return weekdays[i]; } private int getnumberofdaysofmonth(int i) { return daysofmonth[i]; } public string getitem(int position) { return list.get(position); } @override public int getcount() { return list.size(); } /** * prints month * * @param mm * @param yy */ private void printmonth(int mm, int yy) { log.d(tag, "==> printmonth: mm: " + mm + " " + "yy: " + yy); int trailingspaces = 0; int daysinprevmonth = 0; int prevmonth = 0; int prevyear = 0; int nextmonth = 0; int nextyear = 0; int currentmonth = mm - 1; string currentmonthname = getmonthasstring(currentmonth); daysinmonth = getnumberofdaysofmonth(currentmonth); log.d(tag, "current month: " + " " + currentmonthname + " having " + daysinmonth + " days."); gregoriancalendar cal = new gregoriancalendar(yy, currentmonth, 1); log.d(tag, "gregorian calendar:= " + cal.gettime().tostring()); if (currentmonth == 11) { prevmonth = currentmonth - 1; daysinprevmonth = getnumberofdaysofmonth(prevmonth); nextmonth = 0; prevyear = yy; nextyear = yy + 1; log.d(tag, "*->prevyear: " + prevyear + " prevmonth:" + prevmonth + " nextmonth: " + nextmonth + " nextyear: " + nextyear); } else if (currentmonth == 0) { prevmonth = 11; prevyear = yy - 1; nextyear = yy; daysinprevmonth = getnumberofdaysofmonth(prevmonth); nextmonth = 1; log.d(tag, "**--> prevyear: " + prevyear + " prevmonth:" + prevmonth + " nextmonth: " + nextmonth + " nextyear: " + nextyear); } else { prevmonth = currentmonth - 1; nextmonth = currentmonth + 1; nextyear = yy; prevyear = yy; daysinprevmonth = getnumberofdaysofmonth(prevmonth); log.d(tag, "***---> prevyear: " + prevyear + " prevmonth:" + prevmonth + " nextmonth: " + nextmonth + " nextyear: " + nextyear); } int currentweekday = cal.get(calendar.day_of_week) - 1; trailingspaces = currentweekday; log.d(tag, "week day:" + currentweekday + " " + getweekdayasstring(currentweekday)); log.d(tag, "no. trailing space add: " + trailingspaces); log.d(tag, "no. of days in previous month: " + daysinprevmonth); if (cal.isleapyear(cal.get(calendar.year))) if (mm == 2) ++daysinmonth; else if (mm == 3) ++daysinprevmonth; // trailing month days (int = 0; < trailingspaces; i++) { log.d(tag, "prev month:= " + prevmonth + " => " + getmonthasstring(prevmonth) + " " + string.valueof((daysinprevmonth - trailingspaces + day_offset) + i)); list.add(string .valueof((daysinprevmonth - trailingspaces + day_offset) + i) + "-grey" + "-" + getmonthasstring(prevmonth) + "-" + prevyear); } int startdate = 0; // current month days (int = 1; <= daysinmonth; i++) { log.d(currentmonthname, string.valueof(i) + " " + getmonthasstring(currentmonth) + " " + yy); startdate=getcurrentdayofmonth()-(list.size() % 7); system.out.println("**********"+startdate); if (i == getcurrentdayofmonth()) { list.add(string.valueof(i) + "-blue" + "-" + getmonthasstring(currentmonth) + "-" + yy); break; } else { list.add(string.valueof(i) + "-white" + "-" + getmonthasstring(currentmonth) + "-" + yy); } } for(int k=1;k<=startdate;k++){ system.out.println("item remove"+k); list.remove(0); } // leading month days (int = 0; < list.size() % 7; i++) { log.d(tag, "next month:= " + getmonthasstring(nextmonth)); list.add(string.valueof(i + 1) + "-grey" + "-" + getmonthasstring(nextmonth) + "-" + nextyear); } } /** * note: need implement part given year, month, retrieve * entries sqlite database month. iterate on * list of entries, , datecreated, converted * day. * * @param year * @param month * @return */ private hashmap<string, integer> findnumberofeventspermonth(int year, int month) { hashmap<string, integer> map = new hashmap<string, integer>(); return map; } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { view row = convertview; if (row == null) { layoutinflater inflater = (layoutinflater) _context .getsystemservice(context.layout_inflater_service); row = inflater.inflate(r.layout.screen_gridcell, parent, false); } // reference day gridcell gridcell = (button) row.findviewbyid(r.id.calendar_day_gridcell); gridcell.setonclicklistener(this); // account spacing log.d(tag, "current day: " + getcurrentdayofmonth()); string[] day_color = list.get(position).split("-"); string theday = day_color[0]; string themonth = day_color[2]; string theyear = day_color[3]; if ((!eventspermonthmap.isempty()) && (eventspermonthmap != null)) { if (eventspermonthmap.containskey(theday)) { num_events_per_day = (textview) row .findviewbyid(r.id.num_events_per_day); integer numevents = (integer) eventspermonthmap.get(theday); num_events_per_day.settext(numevents.tostring()); } } // set day gridcell gridcell.settext(theday); gridcell.settag(theday + "-" + themonth + "-" + theyear); log.d(tag, "setting gridcell " + theday + "-" + themonth + "-" + theyear); if (day_color[1].equals("grey")) { gridcell.settextcolor(getresources() .getcolor(r.color.lightgray)); } if (day_color[1].equals("white")) { gridcell.settextcolor(getresources().getcolor( r.color.lightgray02)); } if (day_color[1].equals("blue")) { gridcell.settextcolor(getresources().getcolor(r.color.orrange)); } return row; } @override public void onclick(view view) { string date_month_year = (string) view.gettag(); selecteddaymonthyearbutton.settext("selected: " + date_month_year); log.e("selected date", date_month_year); try { date parseddate = dateformatter.parse(date_month_year); log.d(tag, "parsed date: " + parseddate.tostring()); } catch (parseexception e) { e.printstacktrace(); } } public int getcurrentdayofmonth() { return currentdayofmonth; } private void setcurrentdayofmonth(int currentdayofmonth) { this.currentdayofmonth = currentdayofmonth; } public void setcurrentweekday(int currentweekday) { this.currentweekday = currentweekday; } public int getcurrentweekday() { return currentweekday; } } }
Comments
Post a Comment