Android Question How to create a xcustomlistview by code

Discussion in 'Android Questions' started by mberthe, Jul 6, 2019.

  1. mberthe

    mberthe Member Licensed User

    the project below doesn't work
    Code:
    #Region  Project Attributes
        
    #ApplicationLabel: B4A Example
        
    #VersionCode: 1
        
    #VersionName:
        
    'SupportedOrientations possible values: unspecified, landscape or portrait.
        #SupportedOrientations: unspecified
        
    #CanInstallToExternalStorage: False
    #End Region

    #Region  Activity Attributes
        
    #FullScreen: False
        
    #IncludeTitle: True
        
    #BridgeLogger:true
    #End Region

    Sub Process_Globals
        
    End Sub

    Sub Globals
        
    Private clv2 As CustomListView
        
    Private xwstep As Int=30dip
        
    Private label1 As B4XView
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        clv2.Initialize(
    "main","clv2")
        
    Activity.AddView(clv2.AsView,100dip,100dip,100dip,7*xwstep)
        
    For i=1 To 15
            clv2.Add(CreateListItem(
    $"Item #${i}"$100dip, xwstep), $"Item #${i}"$)
        
    Next
        
    End Sub

    Sub CreateListItem(Text As String, Width As Int, Height As Int) As Panel
        
    Dim p As Panel
        p.Initialize(
    "")
        p.SetLayout(
    00, Width, Height)
        p.LoadLayout(
    "CellItem")
        p.Color=
    Colors.Transparent
        label1.Text = Text
        
    Return p
    End Sub

    Sub Activity_Resume

    End Sub

    Sub Activity_Pause (UserClosed As Boolean)

    End Sub
    The message :

    Error occurred on line: 28 (Main)
    java.lang.RuntimeException: Object should first be initialized (B4XView).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
    at b4a.example.main._activity_create(main.java:389)
    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:144)
    at b4a.example.main.afterFirstLayout(main.java:104)
    at b4a.example.main.access$000(main.java:17)
    at b4a.example.main$WaitForLayout.run(main.java:82)
    at android.os.Handler.handleCallback(Handler.java:743)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:150)
    at android.app.ActivityThread.main(ActivityThread.java:5621)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
    ** Activity (main) Resume **

    Thank's for help
     

    Attached Files:

  2. mberthe

    mberthe Member Licensed User

    YES
     
  3. DonManfred

    DonManfred Expert Licensed User

    Customviews should be loaded from a layout.
    You can create a layout with just the xclv and use this layout whenever you need a clv.
     
    Xicu and Erel like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice