Android Question Trouble with CustomListView layout loading

techknight

Well-Known Member
Licensed User
Longtime User
I am probably doing something wrong, but I am attempting to use the CustomDialog/CustomLayoutView to perform the old "Two Text lines and Image" thing that ListView used to do.

Except in this case, I am only needing one text line so that is what I am doing. I wrote some test code, but its crashing sayin the ImageView/Label isnt initialized, but I am loading the "LogoSlot" layout in each panel which contains the controls. Not sure exactly whats happening,

Code:
B4X:
'Called from specific sports layout classes to set the team logo
Sub SetTeamLogo_Click(Side As Int)

    'Grab the Logos stored in the database, if there are no logos we must silently return without prompting for the Logo selection
    Wait For (ConfigDB.RetrieveAllLogos) Complete (Result As Int)    'Get the logos
    If Result <> 2 Then     'If we didnt get anything out of the database, we mark no logos available and return.
        ScoreEngine.LogosAvailable = False
        ScoreEngine.TeamLogo.GuestLogoSet = False
        ScoreEngine.TeamLogo.HomeLogoSet = False
        ScoreEngine.TeamLogo.TeamLogoHome = 0
        ScoreEngine.TeamLogo.TeamLogoGuest = 0
        Return
    End If
    
    'Build a list of logos stored in the database and display them for the operator to choose
    Dim LogoDialog As CustomLayoutDialog
    LogoDialog.ShowAsync("Choose Team Logo", "", "", "", Null, True)                'Initialize the dialog to be displayed
    LogoDialog.SetSize(60%x, 400dip)
    Wait For Dialog_Ready(pnl As Panel)
    pnl.LoadLayout("CustomDialogLayout")                                                'Load the layout which contains the list view.
    CLVDialog.PressedColor = Colors.ARGB(255, 0x7E, 0xB4, 0xFA)    
    
    CLVDialog.Clear
    
    For Each Logo As TeamLogo In ConfigDB.Logos        
        CLVDialog.Add(CreateListItem(Logo), Logo)
    Next
    
    
    'Wait for the person to pick a slot, or if the dialog is canceled, we never reach here. 
    Wait For CLVDialog_ItemClick (Index As Int, Value As Object)

    'This routine should never evaluate true, but we need to handle it in case it does. 
    If CLVDialog.Size = 0 Then 'We didn't find any availble initialized connection slots
        LogoDialog.CloseDialog(DialogResponse.NEGATIVE)
        Return        
    End If
        
    Log("something")
    
End Sub

Sub CreateListItem(Logo As TeamLogo) As Panel
    Dim ItemPanel As Panel
    Dim imgLogoPicture As ImageView
    Dim lblLogoName As Label

    ItemPanel.Initialize("")
    ItemPanel.LoadLayout("LogoSlot")    
    
    imgLogoPicture.Bitmap = Common.ReturnImageFromBytes(Logo.Logo)    
    lblLogoName.Text = Logo.LogoName
    Return ItemPanel
End Sub

Thoughts?
 

Alexander Stolte

Expert
Licensed User
Longtime User
Dim ItemPanel As Panel
#1 Use B4XView
B4X:
Dim ItemPanel As B4XView = xui.CreatePanel("")
ItemPanel.LoadLayout("LogoSlot")
#2 before you load a layout, you have to set the size of the panel, the B4XView type is perfect for this, as it has a SetLayoutAnimated function, which allows you to set all sizes at once.
B4X:
Dim ItemPanel As B4XView = xui.CreatePanel("")
ItemPanel.SetLayoutAnimated(0,0,0,Root.Width,50dip)
ItemPanel.LoadLayout("LogoSlot")
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
#1 Use B4XView
B4X:
Dim ItemPanel As B4XView = xui.CreatePanel("")

#2 before you load a layout, you have to set the size of the panel, the B4XView type is perfect for this, as it has a SetLayoutAnimated function, which allows you to set all sizes at once.
B4X:
Dim ItemPanel As B4XView = xui.CreatePanel("")
ItemPanel.SetLayoutAnimated(0,0,0,Root.Width,50dip)
ItemPanel.LoadLayout("LogoSlot")

It is giving me the same problem, Object should first be initialized error for the ImageView (since thats the first in the logical order)

B4X:
Sub CreateListItem(Logo As TeamLogo, Width As Int, Height As Int) As B4XView
    Dim ItemPanel As B4XView = xui.CreatePanel("")
    Dim imgLogoPicture As ImageView
    Dim lblLogoName As Label

    ItemPanel.SetLayoutAnimated(0, 0, 0, Width, Height)
    ItemPanel.LoadLayout("LogoSlot")   
   
    imgLogoPicture.Bitmap = Common.ReturnImageFromBytes(Logo.Logo)   
    lblLogoName.Text = Logo.LogoName
    Return ItemPanel
End Sub

I should note the two DIM statements below the B4XView declaration is squigglied out, saying object was not initialized.
1709154162874.png
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
Dim imgLogoPicture As ImageView Dim lblLogoName As Label
What is this actually about?
How is B4X supposed to know that this 2 comes from the layout which is only loaded afterwards?

Do this:
B4X:
Sub CreateListItem(Logo As TeamLogo, Width As Int, Height As Int) As B4XView
    Dim ItemPanel As B4XView = xui.CreatePanel("")

    ItemPanel.SetLayoutAnimated(0, 0, 0, Width, Height)
    ItemPanel.LoadLayout("LogoSlot")   
    
    Dim imgLogoPicture As ImageView = ItemPanel.GetView(0)
    Dim lblLogoName As Label = ItemPanel.GetView(1)
    
    imgLogoPicture.Bitmap = Common.ReturnImageFromBytes(Logo.Logo)   
    lblLogoName.Text = Logo.LogoName
    Return ItemPanel
End Sub

if this is not the right one, then please make an example project, because then we are still missing some information.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
What is this actually about?
How is B4X supposed to know that this 2 comes from the layout which is only loaded afterwards?

Do this:
B4X:
Sub CreateListItem(Logo As TeamLogo, Width As Int, Height As Int) As B4XView
    Dim ItemPanel As B4XView = xui.CreatePanel("")

    ItemPanel.SetLayoutAnimated(0, 0, 0, Width, Height)
    ItemPanel.LoadLayout("LogoSlot")
 
    Dim imgLogoPicture As ImageView = ItemPanel.GetView(0)
    Dim lblLogoName As Label = ItemPanel.GetView(1)
 
    imgLogoPicture.Bitmap = Common.ReturnImageFromBytes(Logo.Logo)
    lblLogoName.Text = Logo.LogoName
    Return ItemPanel
End Sub

if this is not the right one, then please make an example project, because then we are still missing some information.

There isn't any missing information that I can see, its probably something I am missing internally.

I know when I load layouts in other places, the control variables just automagically get initialized. In this instances, it is not. I assume its because the context of the layout initialization isnt being picked up? Not sure...

But i will give the above a try and see what happens.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Ok, the getview did work!

I suppose the context of the layout loading to the panel wasnt kept to initialize the controls.
 
Upvote 0

Alexander Stolte

Expert
Licensed User
Longtime User
I know when I load layouts in other places, the control variables just automagically get initialized. In this instances, it is not. I assume its because the context of the layout initialization isnt being picked up? Not sure...
This only works if you declare it globally in the Class_Globals
 
Upvote 0
Top