Working with objects in a List

consultatech4android

Member
Licensed User
Longtime User
Hello all.

I am a development team here from Brazil, and I'm developing an application with Basic4Android for Sales Force.
What is happening is the following:
Must enter information for multiple items ordered, for it created the class itempedido with their attributes and methods, and instantiate an object ip. For each order item entered, I change the appropriate attributes of the object ip and add it to a list object, and use a table object to display.
It turns out that when adding the second item, the first item of data also change. Adding the third item, the first two are modified, and so on.
From what I understood, it seems that a list object keeps a reference to the ip in position, so if I change object attributes ip, this will be reflected in all positions of the object list.
How do I fix this? I need every position of the object list objects have different ip, because each represents a different item from an application with different products and quantities.

Have searched the forum and Google, but not found a solution.


itempedido Class
B4X:
Sub Class_Globals
    Private PalmVendor As Int
   Private CodigoVenda As Int
   Private CodigoPro As Int
   Private SeqItemVenda As Int
   Private qtdVenda As Double
   Private unitarioVenda As Double
   Private fatorVenda As String 
   Private codigoIntPro As Int
   Private totalLinha As Double
   Private exportado As String
   Private valorTabela As Double
   Private nomePro As String
   Private codigoConhecido As String
   Private feirinha As String
   Private ordemGravacao As Int
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    PalmVendor = 0
   CodigoVenda = 0
   CodigoPro = 0
   SeqItemVenda = 0
   qtdVenda = 0
   unitarioVenda = 0
   fatorVenda = "" 
   codigoIntPro = 0
   totalLinha = 0
   exportado = ""
   valorTabela = 0
   nomePro = ""
   codigoConhecido = ""
   feirinha = ""
   ordemGravacao = 0
End Sub

Public Sub getPalmVendor() As Int 
   Return PalmVendor
End Sub

Public Sub setPalmVendor(nPalmVendor As Int)
   PalmVendor = nPalmVendor
End Sub

Public Sub getCodigoVenda() As Int 
   Return CodigoVenda
End Sub

Public Sub setCodigoVenda(nCodigoVenda As Int)
   CodigoVenda = nCodigoVenda
End Sub

Public Sub getCodigoPro() As Int 
   Return CodigoPro
End Sub

Public Sub setCodigoPro(nCodigoPro As Int)
   CodigoPro = nCodigoPro
End Sub

Public Sub getSeqItemVenda() As Int 
   Return SeqItemVenda
End Sub

Public Sub setSeqItemVenda(nSeqItemVenda As Int)
   SeqItemVenda = nSeqItemVenda
End Sub

Public Sub getQtdVenda() As Double
   Return qtdVenda
End Sub

Public Sub setQtdVenda(nqtdVenda As Double)
   qtdVenda = nqtdVenda
End Sub

Public Sub getCodigoConhecido() As String 
   Return codigoConhecido
End Sub

Public Sub setCodigoConhecido(ccodigoConhecido As String)
   codigoConhecido = ccodigoConhecido
End Sub

Public Sub getCodigoIntPro() As Int 
   Return codigoIntPro
End Sub

Public Sub setCodigoIntPro(ncodigoIntPro As Int)
   codigoIntPro = ncodigoIntPro
End Sub

Public Sub getExportado() As String
   Return exportado
End Sub

Public Sub setExportado(cexportado As String)
   exportado = cexportado
End Sub

Public Sub getFatorVenda() As String
   Return fatorVenda
End Sub

Public Sub setFatorVenda(cfatorVenda As String)
   fatorVenda = cfatorVenda
End Sub

Public Sub getFeirinha() As String
   Return feirinha
End Sub

Public Sub setFeirinha(cfeirinha As String)
   feirinha = cfeirinha
End Sub

Public Sub getNomePro() As String
   Return nomePro
End Sub

Public Sub setNomePro(cnomePro As String)
   nomePro = cnomePro
End Sub

Public Sub getOrdemGravacao() As Int
   Return ordemGravacao
End Sub

Public Sub setOrdemGravacao(nordemGravacao As Int)
   ordemGravacao = nordemGravacao
End Sub

Public Sub getTotalLinha() As Double
   Return totalLinha
End Sub

Public Sub setTotalLinha(ntotalLinha As Double)
   totalLinha = ntotalLinha
End Sub

Public Sub getUnitarioVenda() As Double
   Return unitarioVenda
End Sub

Public Sub setUnitarioVenda(nunitarioVenda As Double)
   unitarioVenda = nunitarioVenda
End Sub

Public Sub getValorTabela() As Double
   Return valorTabela
End Sub

Public Sub setValorTabela(nvalorTabela As Double)
   valorTabela = nvalorTabela
End Sub


Activity
B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
        Dim tbVendaItens As Table
   Dim thVendas As TabHost
   Dim lpedidoitens As List
   Dim ip As itempedido
   ...
        ...
End Sub

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

   ip.Initialize
        ...
        ...

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

...
...
...

Sub btAdd_Click
    If (edCodigoConhecido.Text <> "") AND _
      (edQtdVenda.Text <> "") AND _
      (edUnitarioVenda.Text <> "") Then

      ip.setQtdVenda(edQtdVenda.Text)
      ip.setUnitarioVenda(edUnitarioVenda.Text)
       ip.setTotalLinha( pedidoitens.getQtdVenda() * ip.getUnitarioVenda())
      If ckbFator.Checked Then
         ip.setFatorVenda("S")
      Else
         ip.setFatorVenda("N")
      End If
      
      If ip.getOrdemGravacao() = 0 Then
          ip.setOrdemGravacao(lpedidoitens.Size+1)
         lpedidoitens.Add(ip)
      Else
         lpedidoitens.Set((ip.getOrdemGravacao-1),ip)
      End If

      MontaTabelaItens
      
      ip.Initialize
      
      ApagaItensView
      
   Else
      Msgbox("Dados incompletos.","ATENÇÃO")
   End If
   
End Sub
 

keirS

Well-Known Member
Licensed User
Longtime User
The list stores a reference to the IP object. Your code keeps on referencing the same object. The easiest way to solve this is to re dim the ip object in the btAdd_click event. This will create an new object.
 
Upvote 0

consultatech4android

Member
Licensed User
Longtime User
The list stores a reference to the IP object. Your code keeps on referencing the same object. The easiest way to solve this is to re dim the ip object in the btAdd_click event. This will create an new object.


I appreciate the answer, but I found a solution in which I was accustomed to working with Delphi. I created a method called Assign, in which classes in Object Pascal (Delphi) is called to copy all properties from one object to another, provided they are of the same class. Thus the class itempedido was as follows:

B4X:
'Class module
Sub Class_Globals
   Private PalmVendor As Int
   Private CodigoVenda As Int
   Private CodigoPro As Int
   Private SeqItemVenda As Int
   Private qtdVenda As Double
   Private unitarioVenda As Double
   Private fatorVenda As String 
   Private codigoIntPro As Int
   Private totalLinha As Double
   Private exportado As String
   Private valorTabela As Double
   Private nomePro As String
   Private codigoConhecido As String
   Private feirinha As String
   Private ordemGravacao As Int
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
   PalmVendor = 0
   CodigoVenda = 0
   CodigoPro = 0
   SeqItemVenda = 0
   qtdVenda = 0
   unitarioVenda = 0
   fatorVenda = "" 
   codigoIntPro = 0
   totalLinha = 0
   exportado = ""
   valorTabela = 0
   nomePro = ""
   codigoConhecido = ""
   feirinha = ""
   ordemGravacao = 0
End Sub

Public Sub Assign(Snd As itempedido)
   If Snd Is itempedido Then
          PalmVendor      = Snd.getPalmVendor()
      CodigoVenda     = Snd.getCodigoVenda()
      CodigoPro       = Snd.getCodigoPro()
      SeqItemVenda    = Snd.getSeqItemVenda()
      qtdVenda        = Snd.getQtdVenda()
      unitarioVenda   = Snd.getUnitarioVenda()
      fatorVenda      = Snd.getFatorVenda()
      codigoIntPro    = Snd.getCodigoIntPro()
      totalLinha      = Snd.getTotalLinha()
      exportado       = Snd.getExportado()
      valorTabela     = Snd.getValorTabela()
      nomePro         = Snd.getNomePro()
      codigoConhecido = Snd.getCodigoConhecido()
      feirinha        = Snd.getFeirinha()
      ordemGravacao   = Snd.getOrdemGravacao()
   End If
End Sub

Public Sub getPalmVendor() As Int 
   Return PalmVendor
End Sub

Public Sub setPalmVendor(nPalmVendor As Int)
   PalmVendor = nPalmVendor
End Sub

Public Sub getCodigoVenda() As Int 
   Return CodigoVenda
End Sub

Public Sub setCodigoVenda(nCodigoVenda As Int)
   CodigoVenda = nCodigoVenda
End Sub

Public Sub getCodigoPro() As Int 
   Return CodigoPro
End Sub

Public Sub setCodigoPro(nCodigoPro As Int)
   CodigoPro = nCodigoPro
End Sub

Public Sub getSeqItemVenda() As Int 
   Return SeqItemVenda
End Sub

Public Sub setSeqItemVenda(nSeqItemVenda As Int)
   SeqItemVenda = nSeqItemVenda
End Sub

Public Sub getQtdVenda() As Double
   Return qtdVenda
End Sub

Public Sub setQtdVenda(nqtdVenda As Double)
   qtdVenda = nqtdVenda
End Sub

Public Sub getCodigoConhecido() As String 
   Return codigoConhecido
End Sub

Public Sub setCodigoConhecido(ccodigoConhecido As String)
   codigoConhecido = ccodigoConhecido
End Sub

Public Sub getCodigoIntPro() As Int 
   Return codigoIntPro
End Sub

Public Sub setCodigoIntPro(ncodigoIntPro As Int)
   codigoIntPro = ncodigoIntPro
End Sub

Public Sub getExportado() As String
   Return exportado
End Sub

Public Sub setExportado(cexportado As String)
   exportado = cexportado
End Sub

Public Sub getFatorVenda() As String
   Return fatorVenda
End Sub

Public Sub setFatorVenda(cfatorVenda As String)
   fatorVenda = cfatorVenda
End Sub

Public Sub getFeirinha() As String
   Return feirinha
End Sub

Public Sub setFeirinha(cfeirinha As String)
   feirinha = cfeirinha
End Sub

Public Sub getNomePro() As String
   Return nomePro
End Sub

Public Sub setNomePro(cnomePro As String)
   nomePro = cnomePro
End Sub

Public Sub getOrdemGravacao() As Int
   Return ordemGravacao
End Sub

Public Sub setOrdemGravacao(nordemGravacao As Int)
   ordemGravacao = nordemGravacao
End Sub

Public Sub getTotalLinha() As Double
   Return totalLinha
End Sub

Public Sub setTotalLinha(ntotalLinha As Double)
   totalLinha = ntotalLinha
End Sub

Public Sub getUnitarioVenda() As Double
   Return unitarioVenda
End Sub

Public Sub setUnitarioVenda(nunitarioVenda As Double)
   unitarioVenda = nunitarioVenda
End Sub

Public Sub getValorTabela() As Double
   Return valorTabela
End Sub

Public Sub setValorTabela(nvalorTabela As Double)
   valorTabela = nvalorTabela
End Sub


To take advantage of this implementation, the object lpedidoitens became an Array of itempedido.
The encoding of the button's Click event looked like this:


B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
   Dim tbVendaItens As Table
   Dim thVendas As TabHost
   Dim lpedidoitens(199) As itempedido
   Dim ip As itempedido
   ...
   ...
End Sub


...
...
...

Sub btAdd_Click
    If (edCodigoConhecido.Text <> "") AND _
       (edQtdVenda.Text <> "") AND _
       (edUnitarioVenda.Text <> "") Then

        ip.setQtdVenda(edQtdVenda.Text)
        ip.setUnitarioVenda(edUnitarioVenda.Text)
        ip.setTotalLinha( pedidoitens.getQtdVenda() * ip.getUnitarioVenda())
        If ckbFator.Checked Then
            ip.setFatorVenda("S")
        Else
            ip.setFatorVenda("N")
        End If
        
   If pedidoitens.getOrdemGravacao() = 0 Then
      If MaxItem < lpedidoitens.Length Then
         MaxItem = MaxItem + 1
         pedidoitens.setOrdemGravacao(MaxItem+1)
         lpedidoitens(MaxItem).Assign(ip)
      Else
         Msgbox("Número Máximo de itens por pedido atingido.","ATENÇÃO")
      End If
   Else
      lpedidoitens(pedidoitens.getOrdemGravacao-1).Assign(ip)
   End If

        MontaTabelaItens
        
        ip.Initialize
        
        ApagaItensView
       
    Else
        Msgbox("Dados incompletos.","ATEN��O")
    End If
    
End Sub
 
Upvote 0
Top