1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

Android Question Updating Labels in CustomListView

Discussion in 'Android Questions' started by icakinser, May 8, 2019.

  1. icakinser

    icakinser Member Licensed User

    Hi everyone! I have a customlistview loaded with panels that have three labels in them.

    How do I update the text of each label in the panel?

    This is the method i used to populate the list view:
    Code:
    For k = 0 To response.Size - 1
                    
    Dim op As alpacaPosition = response.Get(k)
                    
    Dim p As Panel
                    p.Initialize(
    "")
                    p.SetLayout(
    00, CustomListView2.AsView.Width, 75dip)
                    p.LoadLayout(
    "Item")
                    lbl_symbol.Text = op.symbol & 
    " (" & NumberFormat2(op.currentPrice,1,2,2,True) & ")"
                    
    'Check if there is a loss
                    If IsNegative(op.unrealizedIntradayPL) Then
                        
                        lbl_profit_loss.Text = 
    "$(" & NumberFormat2(op.unrealizedIntradayPL,1,2,2,True) & ")"
                        p.Color = xui.Color_Red
                        
                    
    Else
                        
                        lbl_profit_loss.Text = 
    "$" & NumberFormat2(op.unrealizedIntradayPL,1,2,2,True)
                    p.Color = xui.Color_Green   
                    
    End If   
                    
                    lbl_Qty.Text = 
    NumberFormat2(op.quantity,1,2,2,True)
                    CustomListView2.Add(p,op.symbol)

                
    Next
     
  2. DonManfred

    DonManfred Expert Licensed User

    José J. Aguilar likes this.
  3. icakinser

    icakinser Member Licensed User

  4. icakinser

    icakinser Member Licensed User

    I can only find how to update a control if another control has been clicked :( If i cycle through the CustomListView is it possible to update the label I need?
     
  5. mangojack

    mangojack Well-Known Member Licensed User

    You should have at least posted your attempted effort for someone to guide / correct you as suggested above. ;)

    Code:
    Sub clv1_ItemClick(Index As Int, Value As Object)   
        
    Dim pnl As B4XView = clv1.GetPanel(Index)
        pnl.GetView(
    0).Text = "abc123"   'view(0) = 1st label on panel.. refer order in Designer ViewTree for Item/Row layout
        '...........
     
    José J. Aguilar likes this.
  6. icakinser

    icakinser Member Licensed User

    Sorry my Internet took a poopy. I used
    Code:
    For k = 0 To response.Size - 1
                    
    Dim op As alpacaPosition = response.Get(k)
                    
                    
    Dim pnl As B4XView = clv1.GetPanel(k)
                    pnl.GetView(
    0).Text = op.symbol & " (" & NumberFormat2(op.currentPrice,1,2,2,True) & ")"               
                    
    'Check if there is a loss
                 If IsNegative(op.unrealizedIntradayPL) Then
                        
                        pnl.GetView(
    1).Text = "$(" & NumberFormat2(op.unrealizedIntradayPL,1,2,2,True) & ")"
                       
    ' p.Color = xui.Color_Red
                        
                    
    Else
                        pnl.GetView(
    1).Text = "$" & NumberFormat2(op.unrealizedIntradayPL,1,2,2,True)
                    
                       
    'p.Color = xui.Color_Green   
                   
                  
    End If   
                    
                    pnl.GetView(
    2).Text = NumberFormat2(op.quantity,1,2,2,True)
                    
                
    Next
    Still clunky and very slow
     
  7. icakinser

    icakinser Member Licensed User

    I think I'll just combine the IsNegative into the Sub to get rid of the jump.
     
  8. icakinser

    icakinser Member Licensed User

    Nope doesn't work. Have to rack out another one
     
  9. José J. Aguilar

    José J. Aguilar Well-Known Member Licensed User

    Try it in Release mode.

    Not sure if I'm wrong with this, but try a Sleep(0) o Sleep(50) after update the labels to allow the UI to be refreshed.
     
    icakinser likes this.
  10. icakinser

    icakinser Member Licensed User

    Here is my finished sub for those who need it:
    Code:
    If first_time = True Then
        
    If httpCode = alpaca.HTTP_CODE_OK Then
            
    'addMemo("List open positions: Success! Items retrieved: " & response.Size)
            For k = 0 To response.Size - 1
                
    Dim op As alpacaPosition = response.Get(k)
                
    Dim p As Panel
                p.Initialize(
    "")
                p.SetLayout(
    00, CustomListView2.AsView.Width, 75dip)
                p.LoadLayout(
    "Item")
                lbl_symbol.Text = op.symbol & 
    " (" & NumberFormat2(op.currentPrice,1,2,2,True) & ")"
                
    'Check if there is a loss
                If IsNegative(op.unrealizedIntradayPL) Then
                        
                    lbl_profit_loss.Text = 
    "$(" & NumberFormat2(op.unrealizedIntradayPL,1,2,2,True) & ")"
                    p.Color = xui.Color_Red
                        
                
    Else
                        
                    lbl_profit_loss.Text = 
    "$" & NumberFormat2(op.unrealizedIntradayPL,1,2,2,True)
                    p.Color = xui.Color_Green
                
                
    End If
                    
                lbl_Qty.Text = 
    NumberFormat2(op.quantity,1,2,2,True)
                
                CustomListView2.Add(p,op.symbol)
    Next
                alpaca.fetchAccount
            
    End If
        
        
    Else
            
    If httpCode = alpaca.HTTP_CODE_OK Then
            
    'addMemo("List open positions: Success! Items retrieved: " & response.Size)
            For k = 0 To response.Size - 1
            
    Dim op As alpacaPosition = response.Get(k)
            
    Dim pnl As B4XView = CustomListView2.GetPanel(k)
                    pnl.GetView(
    0).Text = op.symbol & " (" & NumberFormat2(op.currentPrice,1,2,2,True) & ")"
                    
            
    'Check if there is a loss
            If IsNegative(op.unrealizedIntradayPL) Then
                        
                        pnl.GetView(
    1).Text = "$(" & NumberFormat2(op.unrealizedIntradayPL,1,2,2,True) & ")"
                pnl.Color = xui.Color_Red
                        
            
    Else
                        
                        pnl.GetView(
    1).Text = "$" & NumberFormat2(op.unrealizedIntradayPL,1,2,2,True)
                pnl.Color = xui.Color_Green
                
            
    End If
                    
                    pnl.GetView(
    2).Text = NumberFormat2(op.quantity,1,2,2,True)
            
    Next
                
                
            
            
    End If
                
    End If
    You don't need to use the Click event as long as you can cycle through the index array. Runs pretty quick in release mode. But gets a lil slow after about 200 panels.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice