Android Question Retrieves the value from a cardview list

Giovanniluca Vuono

Member
Licensed User
Longtime User
Based on this post https://www.b4x.com/android/forum/threads/cards-list-with-customlistview.87720/#content, is it possible to retrieves or changes the value from a label control of a cardview list? If my label is named Label2, how can I change the text?
For example:
B4X:
Public Sub lista()
    Dim Table1 As List
    Try
        Table1 = DBUtils.ExecuteMemoryTable(Starter.SQL, "SELECT c, datadoc, imp, num FROM S", _
            Null, 0)
    Dim Cols() As String
    Dim bitmaps As List = Array("hand.png", "calc.png")
    For i = 0 To Table1.Size - 1
        tmpval = Table1
        Cols = Table1.Get(i)   
        Log(Cols(1) & " " & Cols(0) & " " & Cols(2))
        Dim content As String = $"Some text here.."$
        'CARDLV1.Add(CreateItem(CARDLV1.AsView.Width, $"Cod. Cliente: #${Cols(0)} - ${Starter.SaldiRagsoc}"$, bitmaps.Get(0), content), "")
        CARDLV1.Add(CreateItem(CARDLV1.AsView.Width, $"Cod. Cliente: #${Cols(0)}"$, bitmaps.Get(0), content, Cols(2), Cols(0)), "")
    Next
    Catch
        Log(LastException.Message)
    End Try
End Sub
Private Sub CreateItem(Width As Int, Title As String, Image As String, Content As String, importo As String, index As Int) As Panel
    Dim p As B4XView = xui.CreatePanel("")
    Dim height As Int = 280dip
    If GetDeviceLayoutValues.ApproximateScreenSize < 4.5 Then height = 310dip
    p.SetLayoutAnimated(0, 0, 0, Width, height)
    p.LoadLayout("Card1")
    lblTitle.Text = Title
    lblContent.Text = Content
    'SetColorStateList(lblAction1, xui.Color_LightGray, lblAction1.TextColor)
    SetColorStateList(lblAction2, xui.Color_LightGray, lblAction2.TextColor)
    'SetTextList(imp, Label2.Text)
    ImageView1.SetBitmap(xui.LoadBitmapResize(File.DirAssets, Image, ImageView1.Width, ImageView1.Height, True))
    imp.Text = 0
    Label2.Text = "Importo € : " & importo
    tmpimp = importo.Replace(",",".")
    Label1.Text = tmpimp
    Label2.Tag = index
    Log(Label2.Tag)
    Return p
End Sub
Sub imp_TextChanged (Old As String, New As String)
    Dim Cols() As String 
    Dim totimp As Double
    Dim index As Int = CARDLV1.GetItemFromView(Sender)
   
    If Old <> New Then
    Cols = tmpval.Get(index)
    Try
        totimp =  CDbl(Cols(2).Replace(",",".")) - New
    Catch
        New = 0
        totimp =  CDbl(Cols(2).Replace(",",".")) - New
    End Try
    Label2.Text = "Importo da saldare € : " &  totimp '<----- text to change
    End If
End Sub
I'd like to change the text of a 6th cardview..
Any suggestions?
Thanx!
 

mangojack

Expert
Licensed User
Longtime User
Had a quick look at the Card ListView..
the example uses a label click to get the index of the parent panel ... 6th cardview would be index#5.

If you want to update the label from within a EditText TextChange event , you would need to have a global variable to hold the index of the card to be updated

B4X:
Sub imp_TextChanged (Old As String, New As String)
 
    '............... more code
    'Dim index As Int = CARDLV1.GetItemFromView(Sender)  'used to get panel index from label click

    Dim pnl As B4XView = CARDLV1.GetPanel(intPanelIndex)   'intPanelIndex , Global Variable somehow updated prior to EditText Change ...
    Dim lbl As B4XView = pnl.GetView(1)      ' 1 = 2nd view added to Panel . *See the Designer View Tree for order of views added to card panel
    lbl.Text = "Importo da saldare € : " &  totimp


Edit ... just realized , if EditText imp is also a view on the cards ... you might be able to use the Focus Changed Event to get (Sender) and set the Card index variable.
ie: ... intPanelIndex = CARDLV1.GetItemFromView(Sender)
 
Last edited:
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Update ... Disregard all of the above . Have more time to look / test the above code .. I cannot get it to work .
It should work , as it is just a CustomListView with a different layout ? .. but it keeps erroring out - Object is not initialized

I have tried declaring the label as both Label / B4XView but still no ..

Hopefully someone will come up with the correct code .


On a side note ... I do not think it a great idea doing this within TextedChanged event ... on every change the code would be run .
Possibly better when the EditText loses Focus maybe.
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Discovered there is a secondary base panel ... so the following code does work.

B4X:
Dim index As Int = CARDLV1.GetItemFromView(Sender) ' or Getting index from Global Var , as described above
  
Dim p1 As B4XView = CARDLV1.GetPanel(index)
Dim p2 As B4XView = p1.GetView(0)  'card view panel  
Dim lbl As B4XView = p2.GetView(1)  '2nd view on panel layout view tree. (designer)
lbl.Text = "Importo da saldare € : " &  totimp

Again ... I dont think it is wise to be running this in an EditText TextedChanged event.
 
Upvote 0

Giovanniluca Vuono

Member
Licensed User
Longtime User
Discovered there is a secondary base panel ... so the following code does work.

B4X:
Dim index As Int = CARDLV1.GetItemFromView(Sender) ' or Getting index from Global Var , as described above
 
Dim p1 As B4XView = CARDLV1.GetPanel(index)
Dim p2 As B4XView = p1.GetView(0)  'card view panel 
Dim lbl As B4XView = p2.GetView(1)  '2nd view on panel layout view tree. (designer)
lbl.Text = "Importo da saldare € : " &  totimp

Again ... I dont think it is wise to be running this in an EditText TextedChanged event.

Hi mangojack, thanx a lot for your suggestions..
In fact I didn't understand what the Panel's GetView was for. Now I have solved.
I've changed the event with FocusChanged too, although TextChanged event worked well..
 
Upvote 0
Top