Android Question CustomListView access using DDD

Tareq Khan

Member
Licensed User
Longtime User
I made a long layout CellItem which has Label1 and CustomListView1. The layout script has DDD.CollectViewsData.

I can access Label1 using
B4X:
dd.GetViewByName(p, "Label1").Text = Text


But, I can't access CustomListView1 using
B4X:
Dim my_clv As CustomListView = dd.GetViewByName(p, "CustomListView1").Tag
my_clv.Clear

I am getting error:

B4X:
Error occurred on line: 256 (DDD)
java.lang.IllegalArgumentException: field b4a.example.b4xmainpage._my_clv has type b4a.example3.customlistview, got java.lang.String

Can you please help? Below is the full code:

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private clv2 As CustomListView
    
   
    
    Private dd As DDD
    
End Sub

Public Sub Initialize
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    dd.Initialize
    'The designer script calls the DDD class. A new class instance will be created if needed.
    'In this case we want to create it ourselves as we want to access it in our code.
    xui.RegisterDesignerClass(dd)
    
    Root.LoadLayout("MainPage")
    
    clv2.Add(CreateListItem("my text", clv2.AsView.Width, 1150dip), "mypage")
    

End Sub


Private Sub CreateListItem(Text As String, Width As Int, Height As Int) As B4XView
    Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0, 0, 0, Width, Height)
    p.LoadLayout("CellItem")
    'Note that we call DDD.CollectViewsData in CellItem designer script. This is required if we want to get views with dd.GetViewByName.
    dd.GetViewByName(p, "Label1").Text = Text
    
    
    
    Dim my_clv As CustomListView = dd.GetViewByName(p, "CustomListView1").Tag
    my_clv.Clear
    
    
    Return p
End Sub
 

stevel05

Expert
Licensed User
Longtime User
It's because CustomListView was one of the earliest custom views and doesn't store it's class in it's tag.

If the code is how it's going to end up, you can create a global for CustomListView1. As the ListItems are created, the global will point at the last CustomListView1 loaded and you can use that.

If you are going to use this again later in the app, you can add the CustomListView Object to the tag of the base of CustomListview1, it will then be available everywhere. This will only be a problem if you are already using the Tag field in the designer for CustomListView1 for something else.

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private clv2 As CustomListView
    Private CustomListView1 As CustomListView

    Private dd As DDD
End Sub


Private Sub CreateListItem(Text As String, Width As Int, Height As Int) As B4XView
    Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0, 0, 0, Width, Height)
    p.LoadLayout("CellItem")
    'Note that we call DDD.CollectViewsData in CellItem designer script. This is required if we want to get views with dd.GetViewByName.
    dd.GetViewByName(p, "Label1").Text = Text
 
    'Update the base tag of the customlistview for future use if required.
    CustomListView1.GetBase.Tag = CustomListView1
 
    'You don't need to use this here, you can access CustomListView1 directly. But just to check it's working
    Dim my_clv As CustomListView = dd.GetViewByName(p, "CustomListView1").Tag
    my_clv.Clear
 
 
    Return p
End Sub
 
Last edited:
Upvote 1

Tareq Khan

Member
Licensed User
Longtime User
No, because CustomListView was one of the earliest custom views and doesn't store it's class in it's tag.

If the code is how it's going to end up, what you can do is create a global for CustomListView1. As the ListItems are created, the global will point at the last CustomListView1 loaded and you can use that.

If you are going to use this again later in the app, you can add the CustomListView Object to the tag of the base of CustomListview1, it will then be available everywhere. This will only be a problem if you are already using the Tag field in the designer for CustomListView1 for something else.

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private clv2 As CustomListView
    Private CustomListView1 As CustomListView

    Private dd As DDD
End Sub


Private Sub CreateListItem(Text As String, Width As Int, Height As Int) As B4XView
    Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0, 0, 0, Width, Height)
    p.LoadLayout("CellItem")
    'Note that we call DDD.CollectViewsData in CellItem designer script. This is required if we want to get views with dd.GetViewByName.
    dd.GetViewByName(p, "Label1").Text = Text
 
    'Update the base tag of the customlistview for future use if required.
    CustomListView1.GetBase.Tag = CustomListView1
 
    'You don't need to use this here, you can access CustomListView1 directly. But just to check it's working
    Dim my_clv As CustomListView = dd.GetViewByName(p, "CustomListView1").Tag
    my_clv.Clear
 
 
    Return p
End Sub
Thanks, It works now!
 
Upvote 0
Top