Android Question B4XListTemplate not supporting option.set

Nitin Joshi

Active Member
Licensed User
Longtime User
Hi, I am using B4XListTemplate to pop up week day list. I want to add index to the list items so i have applied option.set property. It is working fine for B4i however throws error for B4A. Code and Error log as below.

ListTemplate Code:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private Dialog As B4XDialog
    Private Button As Button
    Dim listtemplate As B4XListTemplate
End Sub


Sub Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    
    
End Sub

Public Sub Initialize
    
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("Home_Page")
    Dialog.Initialize(Root)
    listtemplate.Initialize
    listtemplate.AllowMultiSelection = True
    listtemplate.MultiSelectionMinimum = 1
    listtemplate.Options.Set(0, "Sunday")
    listtemplate.Options.Set(1, "Monday")
    listtemplate.Options.Set(2, "Tueday")
    listtemplate.Options.Set(3, "Wednesday")
    listtemplate.Options.Set(4, "Thursay")
    listtemplate.Options.Set(5, "Friday")
    listtemplate.Options.Set(6, "Saturday")

End Sub

Private Sub B4XPage_Appear()

End Sub

Private Sub B4XPage_Resize(Width As Float, Height As Float)
    
End Sub

Private Sub B4XPage_Foreground

End Sub

Private Sub Button_Click
    Dim sf As Object = Dialog.ShowTemplate(listtemplate, "Ok", "", "Cancel")
    Wait For (sf) Complete (Result As Int)
    If Result = xui.DialogResponse_Positive Then
        Log(listtemplate.SelectedItems)
    End If
End Sub

Error log...
Logger connected to: OnePlus IN2011

** Activity (main) Resume **

*** Service (starter) Create ***

** Service (starter) Start **

** Activity (main) Create (first time) **

Error occurred on line: 36 (B4XMainPage)

java.lang.reflect.InvocationTargetException

at java.lang.reflect.Method.invoke(Native Method)

at anywheresoftware.b4a.keywords.Common.CallSubDebug2(Common.java:1087)

at B4X.test.b4xpagesmanager._createpageifneeded(b4xpagesmanager.java:1070)

at B4X.test.b4xpagesmanager._showpage(b4xpagesmanager.java:427)

at B4X.test.b4xpagesmanager._addpage(b4xpagesmanager.java:247)

at B4X.test.b4xpagesmanager._addpageandcreate(b4xpagesmanager.java:261)

at B4X.test.b4xpagesmanager._initialize(b4xpagesmanager.java:167)

at B4X.test.main._activity_create(main.java:419)

at java.lang.reflect.Method.invoke(Native Method)

at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)

at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)

at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)

at java.lang.reflect.Method.invoke(Native Method)

at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)

at B4X.test.main.afterFirstLayout(main.java:105)

at B4X.test.main.access$000(main.java:17)

at B4X.test.main$WaitForLayout.run(main.java:83)

at android.os.Handler.handleCallback(Handler.java:942)

at android.os.Handler.dispatchMessage(Handler.java:99)

at android.os.Looper.loopOnce(Looper.java:240)

at android.os.Looper.loop(Looper.java:351)

at android.app.ActivityThread.main(ActivityThread.java:8423)

at java.lang.reflect.Method.invoke(Native Method)

at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:584)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1013)

Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException

at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:336)

at anywheresoftware.b4a.debug.Debug.CallSubNew2(Debug.java:285)

... 25 more

Caused by: java.lang.reflect.InvocationTargetException

at java.lang.reflect.Method.invoke(Native Method)

at anywheresoftware.b4a.debug.Debug.CallSub4(Debug.java:318)

... 26 more

Caused by: java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0

at jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)

at jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)

at jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)

at java.util.Objects.checkIndex(Objects.java:359)

at java.util.ArrayList.set(ArrayList.java:477)

at anywheresoftware.b4a.objects.collections.List.Set(List.java:123)

at B4X.test.b4xmainpage._b4xpage_created(b4xmainpage.java:83)

... 28 more

** Activity (main) Resume **
 
Solution
B4X:
Dim l1 As List
l1.Initialize
l1.Add("a")
l1.Add("b")
l1.Add("c")
Dim SecondItem As String = l1.Get(1)

The only case where using Set makes sense is if you want to replace an existing item.

Nitin Joshi

Active Member
Licensed User
Longtime User
@Erel, Thank you for your reply. my intention is to fetch list item from 0 to 6 indexed values, so, i am using set and get method. Let me elaborate. I have created array of int (0 to 6). i am setting value in array (1 means day is selected else 0) for the selected item(s) (Day of the week) from the list.
Just re-emphasizing that same code is working well for B4i

You should use Add instead
Do you mean to say that first add and then set?
 
Upvote 0

Nitin Joshi

Active Member
Licensed User
Longtime User
@Erel, i understood the purpose of Set now, thanks.
My take away is, get can fetch the item from list (selected or not selected), no need to use Set.
 
Upvote 0
Top