Android Question Working with List types

Arf

Well-Known Member
Licensed User
Longtime User
'buffers' is a List type, declared like this:
Private buffers As List
'PeakVol' is an int, declared like this:
Dim PeakVol As Int

It gets stuffed with Ints, I can break the debugger and see all the data, but when I try traverse the values in the list like this:
B4X:
    For Each b As Int In buffers
        PeakVol = b
    Next

I get this error on the 'For Each' line:
java.lang.NumberFormatException: Invalid double: "[I@41e54cc8"

I can't quite firgure out what I'm doing wrong. Why is it saying invalid double? Everything is Int.
 

stevel05

Expert
Licensed User
Longtime User
How are you populating buffers?

The safest way is to declare a variable as an Int, then add that to the list, if you are using Array As with AddAll, it should be Array As Int.

Can you show the code that add's the values to buffer.
 
  • Like
Reactions: Arf
Upvote 0

Arf

Well-Known Member
Licensed User
Longtime User
Hi Steve,
'buffers' get populated like this:
B4X:
Sub streamer_RecordBuffer (Buffer() As Byte)

    Dim j As Int    
    j = Buffer.Length / 2
    Dim s As Byte
    Dim t As Byte
    Dim intbuffer(384) As Int
   
    For i = 0 To j - 1
        s = Bit.AND (Buffer( 2 * i ), 255)
        t = Bit.AND ( Buffer( 2 * i + 1), 255)
        'intbuffer(i) = ( s + Bit.ShiftLeft(t, 8) + Bit.AND(t,128) * -512 )
        intbuffer(i) = ( s + Bit.ShiftLeft(t, 8) + Bit.AND(t,255) )
    Next     'that lot was just to convert the bytes to Int audio samples

    buffers.Add(intbuffer)   
   
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Everything is Int.

No, it isn´t! With your code you put an array of int as ONE object in the list
Yo; if you want to use this buffer later you need to get the first object of this list to get the array back.

asuming that buffers is of Type LIST:
i would write it like this
B4X:
Sub streamer_RecordBuffer (Buffer() As Byte)
    Dim j As Int 
    j = Buffer.Length / 2
    Dim s As Byte
    Dim t As Byte
    'Dim intbuffer(384) As Int

    For i = 0 To j - 1
        s = Bit.AND (Buffer( 2 * i ), 255)
        t = Bit.AND ( Buffer( 2 * i + 1), 255)
        Dim intbuf As Int  ' Declare a new Variable of type INT
        'intbuffer(i) = ( s + Bit.ShiftLeft(t, 8) + Bit.AND(t,128) * -512 )
        intbuf = ( s + Bit.ShiftLeft(t, 8) + Bit.AND(t,255) )
        buffers.Add(intbuf) ' Add the int into the list
    Next     'that lot was just to convert the bytes to Int audio samples
    ' You  now can use buffers in your foreach-loop
End Sub
 
Last edited:
Upvote 0
Top