fill the calendar with proper data

This commit is contained in:
tibbi 2016-01-27 20:30:55 +01:00
parent 883f30e361
commit 7641039aec
1 changed files with 24 additions and 10 deletions

View File

@ -16,22 +16,36 @@ public class MainActivity extends AppCompatActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
TableLayout tableHolder = (TableLayout) findViewById(R.id.table_holder); final TableLayout tableHolder = (TableLayout) findViewById(R.id.table_holder);
LayoutInflater inflater = getLayoutInflater(); final LayoutInflater inflater = getLayoutInflater();
DateTime dateTime = new DateTime(); final DateTime now = new DateTime();
final int currMonthDays = dateTime.dayOfMonth().getMaximumValue(); final int currMonthDays = now.dayOfMonth().getMaximumValue();
for (int i = 0; i < 5; i++) { final int firstDayIndex = now.withDayOfMonth(1).getDayOfWeek() - 1;
final int prevMonthDays = now.minusMonths(1).dayOfMonth().getMaximumValue();
final int prevMonthStart = prevMonthDays - firstDayIndex + 1;
int cur = 0;
int thisMonthDays = 1;
int nextMonth = 1;
for (int i = 0; i < 6; i++) {
final TableRow row = (TableRow) inflater.inflate(R.layout.table_row, tableHolder, false); final TableRow row = (TableRow) inflater.inflate(R.layout.table_row, tableHolder, false);
for (int j = 1; j < 8; j++) { for (int j = 0; j < 7; j++) {
final int currDate = i * 7 + j;
if (currDate > currMonthDays)
break;
final TextView day = (TextView) inflater.inflate(R.layout.table_day, row, false); final TextView day = (TextView) inflater.inflate(R.layout.table_day, row, false);
int currDate = thisMonthDays;
if (cur < firstDayIndex) {
currDate = prevMonthStart + cur;
} else if (currDate <= currMonthDays) {
thisMonthDays++;
} else {
currDate = nextMonth++;
}
day.setText(String.valueOf(currDate)); day.setText(String.valueOf(currDate));
row.addView(day); row.addView(day);
cur++;
} }
tableHolder.addView(row); tableHolder.addView(row);