Android Question Panel.GetView results in an error

Haris Hafeez

Active Member
Licensed User
Longtime User
Hello All,

I have been trying to crack this issue but can't.

I have a ScrollView and I'm loading a designer layout into the panel. The designer layout in turn contains a panel which contains the views I want to show, such as labels and a button.

I want to perform an action when the user either clicks the panel for each 'row' of panel items or when the button is clicked. I want to set the tag of the panel itself or the button to a value which i will use as a key to do something. When I use panel.getView to set the tag for the button, the code fails due an initialization error. I can't determine which item is unintialized as the panel item views were added in the designer.

I am at a loss as to how can such a simple requirement result be too difficult. Stuff like this is quite simple in Java(I have strong Java experience) but over here I seem to be missing something obvious and I can't figure it out. Can someone please look at the source below and let me know what I am missing.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("SavedMessages")
    LoadMessages
End Sub

Sub LoadMessages
    Dim keys As List = PA_Messaging_Service.MessageStore.ListKeys
    Dim CurrentTop As Int = 0
    ScrollContainer.Panel.Height = 120dip * keys.Size
    Dim i As Int = 0
    For Each Key As String In keys

        Dim RowPanel As Panel = MakeRowPanelForKey(Key,CurrentTop,i)
        RowPanel.LoadLayout("MessageItemLayout")
        ChangeTagOfEditButton(RowPanel, Key)
        CurrentTop = CurrentTop + 122dip
    Next
End Sub
Sub ChangeTagOfEditButton(RowPanel As Panel, Key As String)
    For i = 0 To RowPanel.NumberOfViews
        Dim v As View = RowPanel.GetView(i)
        If v.Tag = "EditButton" Then
            v.Tag = Key
        End If
    Next
End Sub

Sub MakeRowPanelForKey(Key As String, CurrentTop As String, Index As Int) As Panel
        Dim RowPanel As Panel
        RowPanel.Initialize("HandleRowClick")
        ScrollContainer.Panel.AddView(RowPanel, 0, CurrentTop, 100%x, 120)
        RowPanel.Height = 120dip
        RowPanel.Width = 100%x
        RowPanel.Tag = Key
        If Index Mod 2 = 0 Then
            RowPanel.Color = Colors.LightGray
        Else
            RowPanel.Color = Colors.Black
        End If
        Return RowPanel
End Sub
 

Haris Hafeez

Active Member
Licensed User
Longtime User
Well. It is very late in the night I suppose. I had to change one of the methods as follows. However, I would still appreciate if someone can enlighten me as to what is the best way of achieving this in B4A.
B4X:
Sub ChangeTagOfEditButton(RowPanel As Panel, Key As String)
    For Each v As View In RowPanel.GetAllViewsRecursive
        If v.Tag = "EditButton" Then
            v.Tag = Key
        End If
    Next
End Sub
 
Upvote 0

Haris Hafeez

Active Member
Licensed User
Longtime User
Thanks TDS. My bad :(

But as for the logical way I'm handling the clicks, do you think it is recommended or is there a better way?
 
Upvote 0
Top