Android Question Dialogs2 library always returns day 1 of the month

vecino

Well-Known Member
Licensed User
Longtime User
Hi, I'm testing the "Dialogs2" library and something strange is happening. Whatever date I select, it always returns the 1st of the month.
Example, today is 06.10.2021 and I touch a label that opens the dialog window to choose date, I select any day of any month and accept. The result returns the 1st day of the month.
Sometimes, on the second attempt it returns the selected day correctly, however, on the third attempt it usually fails again.
I attach a video screenshot to help you understand.
The code I'm using is copied from the library demo.
Thank you.

 

agraham

Expert
Licensed User
Longtime User
This is probably happening in Async mode when SpinMonths_ItemClick or SpinYears_ItemClick adjusts the valid number of days and the user does not then reselect a new day value. For some reason resetting the contents of SpinDays resets the selected index but not the displayed value as any sane person would expect! :(

Also I see an unexpected SpinYears_ItemClick event on opening the dialog even when the user has not interacted with it which causes the above effect :(

My God! - Androids' stupid behaviour frustrates me mightily at times :mad:

The solution is to save and restore the selected day as below..
B4X:
Private Sub SpinMonths_ItemClick (Position As Int, Value As Object)
    Dim days As Int = MonthDays(Position)
    Dim year As Int = SpinYears.Selecteditem
    Dim day As Int = spinDays.SelectedIndex + 1
    If year Mod 4 = 0 And spinMonths.SelectedIndex = 1 Then days = days + 1
    spinDays.Clear
    For i = 1 To days
        spinDays.Add(NumberFormat(i, 2, 0))
    Next
    If day > days Then day = days
    spinDays.SelectedIndex = day - 1
End Sub
   
Private    Sub SpinYears_ItemClick (Position As Int, Value As Object)
    Dim days As Int = MonthDays(spinMonths.SelectedIndex)
    Dim year As Int = SpinYears.Selecteditem
    Dim day As Int = spinDays.SelectedIndex + 1
    If year Mod 4 = 0 And spinMonths.SelectedIndex = 1 Then ' year divisible by 4 and
        If Not(year Mod 100 = 0 And year Mod 400 <> 0 ) Then     ' century year whose not divisible by 400.
            days = days + 1
        End If
    End If
    spinDays.Clear
    For i = 1 To days
        spinDays.Add(NumberFormat(i, 2, 0))
    Next
    spinDays.SelectedIndex = day - 1
End Sub
 
Last edited:
Upvote 1
Top