Android Question Insert Panel into Scrollview problem. (SOLVED)

Paolo Pini

Member
Licensed User
Longtime User
Hello,
I have a problem that I can't solve, I hope I can make myself clear.
I have a layout with a Scrollview,




inside which I would like to insert a series of layouts with a text and a button to be able to scroll and select them.



I can fill in the label LblRecord in the layout to be inserted but I can't identify the button being pressed because I can't update either the button tag or the text.

The below routine regularly fills in the label lbl and presents the loaded data on the screen.

To recognise the button I am trying to insert a value in the tag.

B4X:
'***************************
'receive an array with a list of string to show in the label'

Sub AddTextBlock(data() As String)
    Dim top As Int = 0 'SvMessaggi.Panel.Height

    'New panel container for layout "record"
    Dim pnl As Panel
    pnl.Initialize("") ' Init
    pnl.SetLayout(0, 0, SvMessaggi.Width, 290dip)

    ' Load layout "record" into panel
    pnl.LoadLayout("record")

    ' Get Button from layout 'record'
    Dim btn As Button = pnl.GetView(1) ' Assumendo che il Button sia la seconda view nel layout

    ' clear label text
    lbl.Text = ""
   
    For b = 1 To 10
        lbl.Text = lbl.Text & data(b) & CRLF
    Next

    ' Set button tag
    btn.Text = data(11)

    ' Add panel to ScrollView
    SvMessaggi.Panel.AddView(pnl, 0, top, SvMessaggi.Width, pnl.Height)

    ' Update top position for next panel
    top = top + pnl.Height + 10dip ' pad

    ' Update ScrollView panel Height
    SvMessaggi.Panel.Height = top
   
    PnlMessaggio.Visible=True
End Sub

How can I identify the button that is pressed so that I can identify the progressive panel in the scrollview.

Button tag do not update.:
'the tag remains as set in the designer
Sub BtnLabel_Click
    Log("Sender: " & Sender)
    Log("Sender Type: " & GetType(Sender))
   
    Dim btn As Button = Sender

    If btn.Tag <> Null Then
        Log("BTN.TAG: " & btn.Text)
    Else
        Log("Error: BTN.TAG is Null")
    End If
End Sub

I hope I have explained myself.

Many thanks in advance.

Lenny


Translated with DeepL.com (free version)
 

Paolo Pini

Member
Licensed User
Longtime User
You never assign btn.Tag
So it will always be as set in the Designer.
Why don't you switch to CustomListView instead of Scrollview?
Thanks for your reply,
You are right, in the code I posted I was trying to see if I could change the .text but it does not change with the .tag.

I will try it with the CustomListView but wanted to see if I am doing something wrong.

Regards.
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
I think that you are not doing it wrong.
But you are checking for btn.Tag that is without a value (don't know if something is set on designer).
Post your project if possible.
Moving to CustomListView would be just for your comfort, but should not be related with your problem.
 
Upvote 0

Paolo Pini

Member
Licensed User
Longtime User
I corrected the code as you pointed out but unfortunately the problem persists.
I the tag still text I insert in designer.

Correct code:
' Set button tag
    btn.Tag = data(11)


'the tag remains as set in the designer
Sub BtnLabel_Click
    Log("Sender: " & Sender)
    Log("Sender Type: " & GetType(Sender))

    Dim btn As Button = Sender

    If btn.Tag <> Null Then
        Log("BTN.TAG: " & btn.Tag)
   Else
        Log("Error: BTN.TAG is Null")
    End If
End Sub
 
Upvote 0

Paolo Pini

Member
Licensed User
Longtime User
This is the log of

B4X:
Log("Sender: " & Sender)
Log("Sender Type: " & GetType(Sender))
Log("BTN.TAG: " & btn.Tag)

Sender: android.widget.Button{7d4a08f VFED..C.. ........ 32,439-973,753 #11}
Sender Type: android.widget.Button
BTN.TAG: TEST TAG - text tag in the designer
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Molla tutto ed usa una CustomListView, tutto sarà più semplice (e non dimenticare di creare progetti di tipo B4XPages, anche questi molto più convenienti).

Drop everything and use a CustomListView, everything will be easier (and don't forget to create B4XPages type projects, which are also much more convenient).
 
Upvote 1

Paolo Pini

Member
Licensed User
Longtime User
Si, mi sa che mi conviene fare cosi, grazie.

Yes, I guess I'd better do that, thank you.
 
Upvote 0

Paolo Pini

Member
Licensed User
Longtime User
Follow your suggestions I used Customlistview and solved quickly.

CustomListView1 version:
'***************************
Sub AddTextBlock(text() As String)
    Dim xui As XUI
   
    For i = 0 To 5 ‘inserts 5 test panels
        Dim p As Panel
        p=xui.CreatePanel(‘’)
        p.Color = Colors.Transparent
        p.SetLayoutAnimated(0,0,0,CustomListView1.AsView.Width, 300dip)
        p.LoadLayout(‘record’)
       
        ' Get the Label and Button from the loaded layout
        Dim lbl As Label = p.GetView(0) ‘ Assuming the Label is the first view
        Dim btn As Button = p.GetView(1) ‘ Assuming the Button is the second view in the layout

        ' Set the text of the Label
        lbl.Text = ‘’
       
        For b = 1 To 10
            lbl.Text = lbl.Text & text(b) & CRLF
        Next

        ' Set the label's Tag
        lbl.Tag = text(11) ‘ Set the label Tag  
       
        CustomListView1.Add(p,i)
    Next
   
    PnlMessage.Visible=True
    CustomListView1.AsView.Visible=True
End Sub

'***************************
Sub CustomListView1_ItemClick (Index As Int, Value As Object)
    Log(Index)
   
    Dim item As CLVItem = CustomListView1.GetRawListItem(Index)
    Dim p As Panel = item.Panel.GetView(0)
    Dim lbl As Label = p.GetView(1)
   
    Log(lbl.Tag)
End Sub

thank you all

Paolo
 
Last edited:
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
B4X:
' Get the Label and Button from the loaded layout
Dim lbl As Label = p.GetView(0) ‘ Assuming the Label is the first view
Dim btn As Button = p.GetView(1) ‘ Assuming the Button is the second view in the layout
This part is just a pain for you and for your code.
Simply Generate Member of the Label and the Button from your 'record' layout and then
B4X:
'This will be in your Global
Private LblRecord as Label
Private BtnLabel as Button


'***************************
Sub AddTextBlock(text() As String)
    Dim xui As XUI
 
    For i = 0 To 5 ‘inserts 5 test panels
        Dim p As Panel
        p=xui.CreatePanel(‘’)
        p.Color = Colors.Transparent
        p.SetLayoutAnimated(0,0,0,CustomListView1.AsView.Width, 300dip)
        p.LoadLayout(‘record’)

        ' Set the text of the Label
        LblRecord.Text = ""
    
        For b = 1 To 10
            LblRecord.Text = LblRecord.Text & text(b) & CRLF
        Next

        ' Set the label's Tag
        LblRecord.Tag = text(11) ‘ Set the label Tag
    
        CustomListView1.Add(p,i)
    Next
 
    PnlMessage.Visible=True
    CustomListView1.AsView.Visible=True
End Sub
Also I would not create 5 panels in one call, I would call 5 times the Sub that create 1 panel.
But it was a test so probably you made it on purpose.
 
Upvote 0

Paolo Pini

Member
Licensed User
Longtime User

this simplifies the code and makes it clearer, thank you.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…