Android Question For / Next Loop

Declan

Well-Known Member
Licensed User
Longtime User
I am populating a panel as a month view calendar.
Within the panel I have panels 7 across X 6 down to form a "grid" of 42 panels.
Each panel has a Label that will represent the Date.
The labels are named, top left "lblP1" to bottom right "lblP42"
In order to start the first day of the month at the correct position, I am using the following code:
B4X:
    If FirstDayName = "Saturday" Then   
        lblP7.Text = "1"
        lblP8.Text = "2"
        lblP9.Text = "3"
        lblP10.Text = "4"
        lblP11.Text = "5"   
        lblP12.Text = "6"
        lblP13.Text = "7"
        lblP14.Text = "8"
        lblP15.Text = "9"
        lblP16.Text = "10"
        lblP17.Text = "11"
        lblP18.Text = "12"
        lblP19.Text = "13"
        lblP20.Text = "14"
        lblP21.Text = "15"
        lblP22.Text = "16"
        lblP23.Text = "17"
        lblP24.Text = "18"
        lblP25.Text = "19"
        lblP26.Text = "20"
        lblP27.Text = "21"
        lblP28.Text = "22"
        lblP29.Text = "23"
        lblP30.Text = "24"
        lblP31.Text = "25"
        lblP32.Text = "26"
        lblP33.Text = "27"
        lblP34.Text = "28"
        lblP35.Text = "29"
        lblP36.Text = "30"
    End If
Is there a more efficient way of doing this?
Possible with a For.....Next Loop using:
B4X:
DateUtils.NumberOfDaysInMonth(MyMonth, MyYear)

Image from above code:
CalendarTest.png
 

sorex

Expert
Licensed User
Longtime User
yes

use an array of labels

clear all text

figure out what weekday the 1st is on

start filling from there to there+daysinmonth with a loop
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
yes

use an array of labels

clear all text

figure out what weekday the 1st is on

start filling from there to there+daysinmonth with a loop
All views are created with the Designer.
I know what the weekday is of the 1st using stringFunctions:
B4X:
    MyDayOfMonth = sf.DayOfMonth(MyToday)
    MyWeekDay = sf.WeekName (MyToday)
    MyMonth = sf.Month(MyToday)
    MyMonthName = sf.MonthName(MyToday)
    MyYear = sf.Year(MyToday)
   
    FirstDayDate =  "01/" & MyMonth & "/" & MyYear
   
    FirstDayName = sf.WeekName(FirstDayDate)
How would I create an array of labels?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
You have 42 labels

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
   
    Private lblP1 As Label
    Private lblP2 As Label
    Private lblP3 As Label
    ' and the others too... up to lblP42

    Private calLabels(42) As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    calLabels  = Array As Label(lblP1,lblP2,lblP3, ...)
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
I personally never use the designer so I don't know if you can assign it there.

an option might be

B4X:
dim days(42) as label=array as label(lblP1,lblP2,lblP3,lblP4,lblP5 ...)

then you can use something like

B4X:
for x=1 to monthdays
days(weekday(firstdate)+x).text=x
next
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
Ok, I have:
B4X:
    calLabels  = Array As Label(lblP1,lblP2,lblP3,lblP4,lblP5,lblP6,lblP7,lblP8,lblP9,lblP10,lblP11,lblP12, _
                                lblP13,lblP14,lblP15,lblP16,lblP17,lblP18,lblP19,lblP20,lblP21,lblP22,lblP23, _
                                lblP24,lblP25,lblP26,lblP27,lblP28,lblP29,lblP30,lblP31,lblP32,lblP33,lblP34, _
                                lblP35,lblP36,lblP37,lblP38,lblP39,lblP40,lblP41,lblP42)

The following works:
B4X:
    If FirstDayName = "Saturday" Then
        For x=1 To DaysInMonth
            calLabels((5)+x).text=x <<----This is edited to make it work but is not correct
        Next
    End If
But what is the correct way to call:
for x=1 to monthdays
days(weekday(firstdate)+x).text=x
next
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
you don't need the firstdayname check.

you need to get the weekday number.

this acts as an offset so that it possitions right in the array.

let me open B4A to look at the date options.
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
@sorex I have attached my project
This line: ' FirstDayName = "Tuesday"
is to test different month start days
 

Attachments

  • CalendarTest.zip
    15.5 KB · Views: 127
Upvote 0

sorex

Expert
Licensed User
Longtime User
this seems to work fine

B4X:
Sub Globals
Dim days(42) As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
DateTime.DateFormat="yyyy-MM-dd"
setup
update(4,2017) 'update to april 2017
End Sub

Sub setup
For x=0 To 41
    Dim l As Label
    l.Initialize("")
    l.Text="x" 'just for texting the setup
    l.Gravity=Gravity.CENTER
    days(x)=l
    Activity.AddView(l,(x Mod 7) * 14%x,Floor(x/7)*5%x,14%x,7%x)
Next
End Sub

Sub update(m As Int,y As Int)
Dim wd,md,dt As Long
For x=0 To 41
   days(x).text=""
Next
dt=DateTime.DateParse(y&"-"&m&"-01")
wd=DateTime.GetDayOfWeek(dt)-1
md=DateTime.GetDayOfMonth(DateTime.Add(dt,0,1,0)-DateTime.TicksPerDay)
For x=0 To md-1
   days(wd+x).Text=x+1
Next  
End Sub
 
Last edited:
Upvote 0
Top