Android Question Existance of .Get check question

Hobby4Life

Member
Licensed User
Hi all

I have question about how to check the existance of the last list item.

B4X:
        TimeSlot_Time = List1.Get(11)

I have added this line, but running the app on a device that doesnt have this list position saved first, will end up in crashing the app..

How to catch this,, and replace by default? or better give error flag?

B4X:
Sub LoadINI
    Dim List1 As List

    If File.Exists(File.DirRootExternal,FileINI) Then
        List1 = File.ReadList(File.DirRootExternal,FileINI)
   
        Timeslot_Spinner1.SelectedIndex = List1.Get(0)
        Timeslot_Spinner2.SelectedIndex = List1.Get(1)
        Timeslot_Spinner3.SelectedIndex = List1.Get(2)
        Timeslot_Spinner4.SelectedIndex = List1.Get(3)
        Timeslot_Spinner5.SelectedIndex = List1.Get(4)
        Timeslot_Spinner6.SelectedIndex = List1.Get(5)
        Timeslot_Spinner7.SelectedIndex = List1.Get(6)
        Timeslot_Spinner8.SelectedIndex = List1.Get(7)
        Timeslot_Spinner9.SelectedIndex = List1.Get(8)
        Timeslot_Spinner10.SelectedIndex = List1.Get(9)
        Buzzer_Spinner.SelectedIndex = List1.Get(10)
        TimeSlot_Time = List1.Get(11)
       
    End If
End Sub
 

stevel05

Expert
Licensed User
Longtime User
You could enclose the code in a Try Catch block, but to help properly, we would need to understand more about the process, and why it would fail in the first place.
 
Upvote 0

Hobby4Life

Member
Licensed User
Why it happens is easy to understand in de following code when the list is saved..

In the old version of my app you will noticed TimeSlot_Time variable is not saved.
In my new app version I am trying to read that list position.. therefore it crashes because there is nothing written on that place.



Save File version 1.0

B4X:
Sub SaveINI
    Dim List1 As List

    List1.Initialize
   
    List1.Add(Timeslot_Spinner1.SelectedIndex)
    List1.Add(Timeslot_Spinner2.SelectedIndex)
    List1.Add(Timeslot_Spinner3.SelectedIndex)
    List1.Add(Timeslot_Spinner4.SelectedIndex)
    List1.Add(Timeslot_Spinner5.SelectedIndex)
    List1.Add(Timeslot_Spinner6.SelectedIndex)
    List1.Add(Timeslot_Spinner7.SelectedIndex)
    List1.Add(Timeslot_Spinner8.SelectedIndex)
    List1.Add(Timeslot_Spinner9.SelectedIndex)
    List1.Add(Timeslot_Spinner10.SelectedIndex)
    List1.Add(Buzzer_Spinner.SelectedIndex)
     
    RP.CheckAndRequest(RP.PERMISSION_WRITE_EXTERNAL_STORAGE)
    wait for Activity_PermissionResult( Permission As String, Result As Boolean)
    If Result Then
        File.WriteList(File.DirRootExternal,FileINI,List1)
        ToastMessageShow ("Instellingen opgeslagen!", True)
    Else
   
    End If

End Sub


Load File version 1.1

B4X:
Sub LoadINI
    Dim List1 As List

    If File.Exists(File.DirRootExternal,FileINI) Then
        List1 = File.ReadList(File.DirRootExternal,FileINI)
   
        Timeslot_Spinner1.SelectedIndex = List1.Get(0)
        Timeslot_Spinner2.SelectedIndex = List1.Get(1)
        Timeslot_Spinner3.SelectedIndex = List1.Get(2)
        Timeslot_Spinner4.SelectedIndex = List1.Get(3)
        Timeslot_Spinner5.SelectedIndex = List1.Get(4)
        Timeslot_Spinner6.SelectedIndex = List1.Get(5)
        Timeslot_Spinner7.SelectedIndex = List1.Get(6)
        Timeslot_Spinner8.SelectedIndex = List1.Get(7)
        Timeslot_Spinner9.SelectedIndex = List1.Get(8)
        Timeslot_Spinner10.SelectedIndex = List1.Get(9)
        Buzzer_Spinner.SelectedIndex = List1.Get(10)
        TimeSlot_Time = List1.Get(11)
       
    End If
End Sub
 
Upvote 0

Hobby4Life

Member
Licensed User
I think I will fix this by using the first list entry as version number..

Read that, if its older.. then call a function that writes new defaults
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
OK, for this type of data I would use a map or KeyValueStore in preference to a List from which you can get data using GetDefault and return a Default value if the Key doesn't exist, So you would do something like:
B4X:
        Timeslot_Spinner1.SelectedIndex = OptionsMap.GetDefault("TSOption1",0)
        Timeslot_Spinner2.SelectedIndex = OptionsMap.GetDefault("TSOption2",0)
        Timeslot_Spinner3.SelectedIndex = OptionsMap.GetDefault("TSOption3",0)
        Timeslot_Spinner4.SelectedIndex = OptionsMap.GetDefault("TSOption4",0)
        Timeslot_Spinner5.SelectedIndex = OptionsMap.GetDefault("TSOption5",0)
        Timeslot_Spinner6.SelectedIndex = OptionsMap.GetDefault("TSOption6",0)
        Timeslot_Spinner7.SelectedIndex = OptionsMap.GetDefault("TSOption7",0)
        Timeslot_Spinner8.SelectedIndex = OptionsMap.GetDefault("TSOption8",0)
        Timeslot_Spinner9.SelectedIndex = OptionsMap.GetDefault("TSOption9",0)
        Timeslot_Spinner10.SelectedIndex = OptionsMap.GetDefault("TSOption10",0)
        Buzzer_Spinner.SelectedIndex = OptionsMap.GetDefault("BZOption1",0)
        TimeSlot_Time = OptionsMap.GetDefault("TimeOption1",DateTime.Now)

Or if you really want to use a list, just check that the index exists and set a default if it doesn't. Something like:
B4X:
If List1.size > 11 Then
    TimeSlot_Time = List1.Get(11)
Else
    TimeSlot_Time = DateTime.Now
End If
 
Last edited:
Upvote 0
Top