Android Question Problem with array of PCLVs

toby

Well-Known Member
Licensed User
Longtime User
My test app is based on this one. I duplicated two more CLVs. I want to use arrays to avoid duplicated codes. However I got "Object should be initialized first" error while calling pclv.initialize. Could someone kindly tell me what I did wrong?

TIA

Sub Globals:
Sub Globals
    Private CustomListView1 As CustomListView
    Private PCLV As PreoptimizedCLV
    Private xui As XUI
    Private Label1 As B4XView
    Private words As List
    Private NumberOfRealItems As Int
   
    'added code:  
    Private CustomListView2 As CustomListView
    Private CustomListView3 As CustomListView
    Private PCLV2, PCLV3 As PreoptimizedCLV
    Private const sectionCount As Int=3
    Private CustomListViews() As CustomListView=Array As CustomListView(CustomListView1, CustomListView2,CustomListView3)
    Private PCLVs() As PreoptimizedCLV=Array As PreoptimizedCLV(PCLV, PCLV2,PCLV3)
End Sub


B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    'Sleep(100)
    words = Array("aaa", "bbb", "ccc", "ddd", "eee", "fff")
    NumberOfRealItems = words.Size
    For j=0 To sectionCount-1
        PCLVs(j).Initialize(Me, "PCLV", CustomListViews(j)) '<=error: "Object should be initialized first"
        PCLVs(j).ShowScrollBar = False
   
        For i = 0 To 500
            PCLVs(j).AddItem(49%x, xui.Color_White, words.Get(i Mod NumberOfRealItems))
        Next
        PCLV.Commit
    Next
    'Sleep(0)
    'CustomListView1.JumpToItem(5000 - (5000 Mod NumberOfRealItems))
End Sub
 

Attachments

  • pclvEendlessLoop3Clvs.zip
    13.1 KB · Views: 147
Solution
You need to move the declaration of the arrays after having loaded the layout.
B4X:
    Activity.LoadLayout("1")
    CustomListViews = Array As CustomListView(CustomListView1, CustomListView2,CustomListView3)
    PCLVs = Array As PreoptimizedCLV(PCLV, PCLV2,PCLV3)

klaus

Expert
Licensed User
Longtime User
You need to move the declaration of the arrays after having loaded the layout.
B4X:
    Activity.LoadLayout("1")
    CustomListViews = Array As CustomListView(CustomListView1, CustomListView2,CustomListView3)
    PCLVs = Array As PreoptimizedCLV(PCLV, PCLV2,PCLV3)
 
Upvote 1
Solution

Mahares

Expert
Licensed User
Longtime User
That looks good, klaus. But I think ony this line needs moved after the layout is loaded, although harmless:
B4X:
CustomListViews = Array As CustomListView(CustomListView1, CustomListView2,CustomListView3)
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I want to use arrays to avoid duplicated codes
If you change your main module code to something like this, you will be able to display all 3 horizontal xCLV 's. Set the event name to all three as clvx, instead of having VisibleRangeChanged (FirstIndex As Int, LastIndex As Int) for each customlistview
B4X:
Sub Activity_Create(FirstTime As Boolean)  
    Activity.LoadLayout("1")
    words = Array("aaa", "bbb", "ccc", "ddd", "eee", "fff")
    NumberOfRealItems = words.Size  
    CustomListViews = Array As CustomListView(CustomListView1, CustomListView2,CustomListView3)
    For j=0 To sectionCount-1
        PCLVs(j).Initialize(Me, "PCLV", CustomListViews(j))
        PCLVs(j).ShowScrollBar = False
        For i = 0 To 500
            PCLVs(j).AddItem(49%x, xui.Color_White, words.Get(i Mod NumberOfRealItems))
        Next
        PCLVs(j).Commit
End Sub

Sub clvx_VisibleRangeChanged (FirstIndex As Int, LastIndex As Int)   'clvx is event name of all 3 xCLV
    Dim clv As CustomListView=Sender
    Dim j As Int=clv.GetBase.Tag
    For Each i As Int In PCLVs(j).VisibleRangeChanged(FirstIndex, LastIndex)
        Dim item As CLVItem = clv.GetRawListItem(i)
        Dim pnl As B4XView = xui.CreatePanel("")      
        item.Panel.AddView(pnl, 0, 0, item.Panel.Width, item.Panel.Height)
        pnl.LoadLayout("Item")
        Label1.Text = item.Value
    Next
End Sub

Sub clvx_ItemClick (Index As Int, Value As Object)
    Log(Index & ", " & Value)
End Sub
 
Last edited:
Upvote 0
Top