Android Question [Solved] How do I get all the data from a List?

Sergio Castellari

Active Member
Licensed User
Suppose I have the following code:
B4X:
  Dim aRegistros, aCampos As List
  aRegistros.Initialize
  aCampos.Initialize
  aCampos.Add(0)
  aCampos.Add("Saldo Inicial")
  aCampos.Add("What?")
  aCampos.Add(1535)
  aRegistros.Add(aCampos)
1) How do I get the content of aRegistros.Get (0) in individual variables?
2) How do I change the content "Initial Balance" for "FINAL Balance"?
 

William Lancee

Well-Known Member
Licensed User
Longtime User
I may be making an big assumption, but I think you are confusing .Add with +

.Add means append a new element (of any kind) to the list. The list after .Add has one more element.

Accessing elements from a list requires a .Get operation.

If you have a list of amounts, the following summarizes the list and presents a running total.

B4X:
    Dim amounts As List
    amounts.Initialize
    amounts.add(0)
    amounts.add(1535)
    amounts.add(205)
    amounts.add(12345)

    Dim cumsum As Float
    For i = 0 To amounts.size - 1
        cumsum = cumsum + amounts.Get(i)
        Log(i & TAB & amounts.Get(i) & TAB & cumsum)
    Next
 
Upvote 0

Sergio Castellari

Active Member
Licensed User
Hola @William Lancee
Thanks for the suggestions.
Google's translation is probably not ideal and ends up with some confusing terms.
I will try to describe my need:
A- I load a first manual "record" of four fields in "aRegistros". Here is an initial balance previously obtained where I must overwrite a DATE in the 3rd. position.
B- Then I make a query to MySQL that returns an "x" amount of records with 4 fields. This query may not even return any records.
C- I needed to take from the FIRST record (from point B), the 3rd field (or position) where a DATE comes from.
D- This DATE should be overwritten in the initial record or zero loaded at point A (as long as the query B obtained at least one record.
I achieved it with the following code (that I show here, so that they tell me if it is well raised or bad):
B4X:
        If aRegistros.Size > 0 Then       'Si existe al menos un movimiento
            Dim aReg0, aReg1 As List
            Dim cFec As String
            aReg0 = aRegistros.Get(0)          'Tomo el reigistro "Saldo Inicial"
            aReg1 = aRegistros.Get(1)                'Tomo el siguiente registro
            cFec = aReg1.Get(2)                            'Obtengo el 3er.campo (fecha)
            aReg0.Set(2,cFec)                                'Sobreescribo el campo fecha
            aRegistros.Set(0,aReg0)                    'Actualizo el registro con el nuevo campo
        End If

With your guidance plus forum searches I was able to do it!
Giant hugs!
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
I understand. Some additional information.

aReg0 is an object that has the same pointer as aRegistros.Get(0)
Since aReg0.Set(2,cFec) already achieves what you want, do you don't need aRegistros.Set(0,aReg0)

Although it does no harm. I sometimes do it just for clarity. You DO need it for primitive values (Int, Long, Float, String, Boolean, Byte) but not for Arrays or lists or Maps.

You also have to be careful that if you have an object rather than a primitive, you need to clone that object to avoid confusing results.

For EXAMPLE if you had a hypothetical situation where cFec was an object rather than a string...
B4X:
Dim cFec() As String = Array As String("EST",  "2021-08-25",  "yyyy-MM-dd")

Then aReg0.Set(2,cFec) would put a pointer to that object in the record's 3rd position, but you probably would want a clone of cFec
A clone would be:
B4X:
Array As String(cFec(0), cFec(1), cFec(2))

If you don't use a clone in this hypothetical situation, then any changes to aReg1.Get(2) would also occur in aReg0.Get(2) since they would point to the same thing.
 
Upvote 0

Sergio Castellari

Active Member
Licensed User
I think I understand...
aReg0 It is a pointer, therefore when modifying it, it is automatically modified toRegisters (0), which would mean that the last instruction "aRegistros.Set (0, aReg0)" is unnecessary
thank you!
 
Upvote 0

emexes

Expert
Licensed User
Google's translation is probably not ideal and ends up with some confusing terms.
No argument there, but nowadays it has the Swap Languages button (Ctrl+Shift+S) that makes it easy to retranslate back to the original language, which will usually highlight any confused terms that might translate better if rephrased more simply or explicitly.

That would have saved me from my most memorable mistranslation, when I asked a non-English-speaking neighbour to:

please tie up the dog on new year's eve (so that she didn't run away when fireworks let off)

but later found out that it had translated to Romanian as:

please put a tie on the dog on new year's eve (close, but no cigar)

1629944483223.png
 
Last edited:
Upvote 0

udg

Expert
Licensed User
Longtime User
please put a tie on the dog on new year's eve (close, but no cigar)
Well, I guess you had a memorable party. A bit more chaotic than desidered, but a lot more elegant :)
 
Upvote 0

emexes

Expert
Licensed User
Well, I guess you had a memorable party.
If you mean the dog and tie... I don't know, we were on the other side of the rock for the Christmas-New Year break. The neighbour (well, the neighbour's dad) was looking after our dog while we were gone.

She was the dog that came with the house when we bought it, and was mighty miffed when we started her sleeping outside of *her* house. ?
 
Upvote 0

Roger Taylor

Member
Licensed User
Longtime User
I may be making an big assumption, but I think you are confusing .Add with +

.Add means append a new element (of any kind) to the list. The list after .Add has one more element.

Accessing elements from a list requires a .Get operation.

If you have a list of amounts, the following summarizes the list and presents a running total.

B4X:
    Dim amounts As List
    amounts.Initialize
    amounts.add(0)
    amounts.add(1535)
    amounts.add(205)
    amounts.add(12345)

    Dim cumsum As Float
    For i = 0 To amounts.size - 1
        cumsum = cumsum + amounts.Get(i)
        Log(i & TAB & amounts.Get(i) & TAB & cumsum)
    Next
Nowhere did he mention a mathematical add.
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
*Off topic:

Hi @Roger Taylor , is that you? (Sorry, you're probably tired of this joke)

27d316e0b42a7f7ba043917ef6105a33-768x768.jpg
 
Upvote 0
Top