java - why isn't my program displaying february 2016 and following months correctly? -


i trying create simple calendar android. supposed show 1 month @ time , list associated events according selected day. stuck on implementation of calendar ui since doesn't list february 2016 , following months correctly.

instead of 29 days lists 28 , listing of february starts on sunday instead of monday. can please take @ program , me?

i suspect error in following lines:

// init temporary calendar monday, first day of displayed month calendar calx= getcalendar(year, month, 1); // week_of_year of first day of month int firstweek = calx.get(calendar.week_of_year); // set day_of_week monday cal.set(calendar.day_of_week, calendar.monday); // set week_of_year first week month part of cal.set(calendar.week_of_year, firstweek); 

i hope not offend posting complete program, unsure bug crawling........

drawable/circle_today.xml:

<?xml version="1.0" encoding="utf-8"?>     <shape xmlns:android="http://schemas.android.com/apk/res/android">         <solid android:color="#ffff00" />         <corners             android:topleftradius="30dp"             android:toprightradius="30dp"             android:bottomleftradius="30dp"             android:bottomrightradius="30dp"             />     </shape> 

drawable/circle_selected.xml:

<?xml version="1.0" encoding="utf-8"?>     <shape xmlns:android="http://schemas.android.com/apk/res/android">         <solid android:color="#00ff00" />         <corners             android:topleftradius="30dp"             android:toprightradius="30dp"             android:bottomleftradius="30dp"             android:bottomrightradius="30dp"             />     </shape> 

layout/activity_main.xml:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"     android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin"     android:paddingright="@dimen/activity_horizontal_margin"     android:paddingtop="@dimen/activity_vertical_margin"     android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".mainactivity">      <tablelayout         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_alignparenttop="true"         android:layout_centerhorizontal="true"         android:id="@+id/tablelayout"></tablelayout>      <listview         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:id="@+id/listview"         android:layout_below="@+id/tablelayout"         android:layout_centerhorizontal="true" />  </relativelayout> 

mainactivity.java:

import android.content.intent; import android.graphics.color; import android.support.v4.app.dialogfragment; import android.support.v4.app.fragmentactivity; import android.os.bundle; import android.support.v7.app.actionbaractivity; import android.support.v7.app.appcompatactivity; import android.view.gravity; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.arrayadapter; import android.widget.edittext; import android.widget.listview; import android.widget.tablelayout; import android.widget.tablerow; import android.widget.textview;  import java.util.arraylist; import java.util.calendar; import java.util.gregoriancalendar;  public class mainactivity extends appcompatactivity { // tablelayout holds calendar matrix private tablelayout calendartable; // list future use list of events of selected day private listview daylist; //list of array strings serve list items arraylist<string> listitems=new arraylist<string>(); //defining string adapter handle data of listview arrayadapter<string> adapter; // today stays today complete lifecycle of mainactivity private int today; // thismonth stays thismonth complete lifecycle of mainactivity private int thismonth; // selectedday changes onclick private int selectedday; // month changes onclick on day of other month private int month; // year changes when other month either january or december of different year private int year;   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     // calendar whith parameters of     calendar calendar = getcalendar();     // initiate global variables     selectedday = today = calendar.get(calendar.day_of_month);     thismonth = month = calendar.get(calendar.month);     year = calendar.get(calendar.year);     // set tablelayout holds calendar matrix     calendartable = (tablelayout) findviewbyid(r.id.tablelayout);     // set list future use list of events of selected day     daylist = (listview) findviewbyid(r.id.listview);     adapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, listitems);     daylist.setadapter(adapter);     // sanity test     testcalendar();     // build calendar table     updatecalendar(); }  // function test calendar behaviour, expected output // week: 8 day: 27 month: 1 // week: 8 day: 28 month: 1 // week: 9 day: 29 month: 1 // week: 9 day: 1 month: 2 // contrary ui behaviour lists 2016-2-1 sunday , lists 28 days private final void testcalendar() {     calendar c = getcalendar(2016, 1, 27);     (int = 0; < 4; i++) {         system.out.println("week: " +c.get(calendar.week_of_year) + " day: " +                 c.get(calendar.day_of_month) + " month: " + c.get(calendar.month));         c.add(calendar.day_of_month, 1);     } }  // function ensure correct configuration of calendar private final calendar getcalendar() {     calendar gc = new gregoriancalendar();     gc.setfirstdayofweek(calendar.monday);     gc.setminimaldaysinfirstweek(4);     return gc; }  // function ensure correct configuration of calendar private final calendar getcalendar(int year, int month, int day) {     calendar gc = new gregoriancalendar(year, month, day);     gc.setfirstdayofweek(calendar.monday);     gc.setminimaldaysinfirstweek(4);     return gc; }  private void updatecalendar() {     // reset table     calendartable.removeallviews();     // init calendar used function     calendar cal = getcalendar();     // init temporary calendar monday, first day of displayed month     calendar calx= getcalendar(year, month, 1);     // week_of_year of first day of month     int firstweek = calx.get(calendar.week_of_year);     // set day_of_week monday     cal.set(calendar.day_of_week, calendar.monday);     // set week_of_year first week month part of     cal.set(calendar.week_of_year, firstweek);     // init reusable vars     tablerow row = null;     textview tv = null;     int paddingnormal = 35;     //outer loop, rows     (int = 0; < 7; i++) {         // init row         row = new tablerow(this);         // stretch row         // code stretch row width parent view, took no effect         /*row.setlayoutparams(new tablerow.layoutparams(tablerow.layoutparams.match_parent,                 tablerow.layoutparams.wrap_content));*/         // inner loop, fields         (int j = 0; j < 8; j++) {             // init             tv = new textview(this);             // center day labels             tv.setgravity(gravity.center_vertical | gravity.center_horizontal);             // code stretch row width parent view, took no effect             /*tv.setlayoutparams(new tablerow.layoutparams(tablerow.layoutparams.wrap_content,                     tablerow.layoutparams.wrap_content));*/             // first row, headers             if (i == 0) {                 tv.setpadding(paddingnormal, paddingnormal, paddingnormal, paddingnormal);                 tv.setbackgroundcolor(color.rgb(213, 213, 213));                 switch (j) {                     case 0:                         break;                     case 1:                         tv.settext("mo");                         break;                     case 2:                         tv.settext("di");                         break;                     case 3:                         tv.settext("mi");                         break;                     case 4:                         tv.settext("do");                         break;                     case 5:                         tv.settext("fr");                         break;                     case 6:                         tv.settext("sa");                         break;                     case 7:                         tv.settext("so");                         break;                 }             }             else if (j == 0) {                 // week no                 tv.setpadding(paddingnormal, paddingnormal, paddingnormal, paddingnormal);                 tv.setbackgroundcolor(color.rgb(213, 213, 213));                 tv.settext(""+cal.get(calendar.week_of_year));             } else {                 // days                 tv.setpadding(paddingnormal, paddingnormal, paddingnormal, paddingnormal);                 tv.settext("" + cal.get(calendar.day_of_month));                 // mark days of different month                 if ((cal.get(calendar.month) != month)) {                     tv.settextcolor(color.rgb(213, 213, 213));                     // switch previous or next month                     tv.setonclicklistener(new view.onclicklistener() {                         @override                         public void onclick(view v) {                             // set selectedday of different month                             selectedday = integer.parseint(((textview) v).gettext() + "");                             // if selectedday bigger 15 part of previous month                             if (selectedday > 15) {                                 // if month january                                 if (month == calendar.january) {                                     // month reset december                                     month = calendar.december;                                     // , year decremented previous year                                     year = year - 1;                                 } else {                                     // else month decremented                                     month = month - 1;                                 }                                 // else selectedday part of next month                             } else {                                 // if month december                                 if (month == calendar.december) {                                     // month reset january                                     month = calendar.january;                                     // , year incremented                                     year = year + 1;                                 } else {                                     // else month incremented                                     month = month + 1;                                 }                             }                             // updatecalendar display different month                             updatecalendar();                         }                     });                 } else {                     // mark today yellow circle                     if ((cal.get(calendar.day_of_month) == today) && month == thismonth) {                         tv.setbackgroundresource(r.drawable.circle_today);                     }                     // mark selected day green circle                     if ((cal.get(calendar.day_of_month) == selectedday)) {                         tv.setbackgroundresource(r.drawable.circle_selected);                     }                     // setonclicklistener updatecalendar , display new selection , daylist                     tv.setonclicklistener(new view.onclicklistener() {                         @override                         public void onclick(view v) {                             // set selectedday before rebuilding table                             selectedday = integer.parseint(((textview) v).gettext() + "");                             // rebuild table                             updatecalendar();                         }                     });                 }                 // increment calendar day_of_month field 1                 cal.add(calendar.day_of_month, 1);             }             // add textview row             row.addview(tv);         }         // add row tablelayout         calendartable.addview(row);     }     // set actionbar title "*month* *year*"     string title ="";     switch(month)     {         case calendar.january:             title = "jänner " + year;             break;         case calendar.february:             title = "februar " + year;             break;         case calendar.march:             title = "märz " + year;             break;         case calendar.april:             title = "april " + year;             break;         case calendar.may:             title = "mai " + year;             break;         case calendar.june:             title = "juni " + year;             break;         case calendar.july:             title = "juli " + year;             break;         case calendar.august:             title = "august " + year;             break;         case calendar.september:             title = "september " + year;             break;         case calendar.october:             title = "oktober " + year;             break;         case calendar.november:             title = "november " + year;             break;         case calendar.december:             title = "dezember " + year;             break;     }     settitle(title);     // list events of selected day     updatedaylist(); }  private void updatedaylist() {     // todo implement...     adapter.clear();     adapter.add(selectedday + "/" + month + "/" + year); }  @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.menu_main, menu);     return true; }  @override public boolean onoptionsitemselected(menuitem item) {     // handle action bar item clicks here. action bar     // automatically handle clicks on home/up button, long     // specify parent activity in androidmanifest.xml.     int id = item.getitemid();      //noinspection simplifiableifstatement     if (id == r.id.action_settings) {         return true;     }     return super.onoptionsitemselected(item); } 

remember day_of_week uses 0=sunday, 1=monday, ... , on. don't know if locale makes difference, in us week starts on sunday.

feb 1, 2015 sunday. week_of_year 6.
setting day_of_week 1=monday monday, feb 2, 2015.
don't think after.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -