Android Question How to set a single element in a list array.

slaw54

Member
Hello, I am a complete newbie to B4A and would appreciate a little guidance. Using examples from the forum I have read a csv file into a list. It consists of approx. 500 rows of six columns, all strings. I can use get to retrieve a row and then an item in the column of that row (as in =array(6,2). How can I set that same item to a different value without affecting the rest of the row as in array(6,2) ="string". The index of set obviously relates to the whole row.
Any thought would be appreciated.
Thanks
 

slaw54

Member
Hi Erel
Thank you for such a speedy reply. I added the two lines suggested but this creates an index out of bounds error. i am trying to convert a meter reading program I originally wrote in visual basic. It simply presents the user with name address old reading etc, the new reading then gets stored in the array for billing. i thought a list would be the answer but I cannot modify a single element in a row. Code is taken from the forum,

B4X:
Sub Button1_Click
xui.MsgboxAsync("Hello world!", "B4X")
    Dim su As StringUtils
    Dim list1 As List
    
    list1 = su.LoadCSV(File.DirAssets, "Book2.csv", ",")
    
    Dim row() As String = list1.Get(1) 'second property in list
    row(1)="4333434" 'new meter reading
    For i = 0 To list1.Size-1
        Dim sCol() As String
        sCol = list1.Get(i)
        Dim NewRow As String
        For il = 0 To sCol.Length-1
            NewRow = NewRow & sCol(il)
            If il < sCol.Length-1 Then
                NewRow = NewRow & " - "
            End If
        Next
        Log(NewRow)
    Next
End Sub
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
I have tested your above code with a similar 6 column csv file.

There was no error and it logged contents as expected.... the first row (column 2) correctly logged the new meter reading.

are you sure you csv file is correct / intact.
 
Upvote 0

slaw54

Member
Apologies Erel, still finding my way around line 51 is For il = 0 To sCol.Length-1
B4X:
Error occurred on line: 51 (Main)
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
    at java.lang.reflect.Array.set(Array.java:454)
    at anywheresoftware.b4a.shell.ArraysUtils.setElement(ArraysUtils.java:84)
    at anywheresoftware.b4a.shell.Shell.setArrayElement(Shell.java:591)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:386)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:197)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:6897)
    at android.widget.TextView.performClick(TextView.java:12727)
    at android.view.View$PerformClick.run(View.java:26101)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6944)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
1 - 410 Horseshoe Bay Rd - 10001a - 104451 - 1257 - 0
2 - 410 Horseshoe Bay Rd - 10001B - 47615 - 615 - 0
 
Upvote 0

slaw54

Member
I have tested your above code with a similar 6 column csv file.

There was no error and it logged contents as expected.... the first row (column 2) correctly logged the new meter reading.

are you sure you csv file is correct / intact.
If I remove
B4X:
row(1)="4333434" 'new meter reading
    For i = 0 To list1.Size-1
the code runs witthout error and list the CSV correctly.
Thanks for the test'
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
if the csv file was not uniform, i.e. each staff does not have an array of the same length, I would try this
B4X:
Sub Button1_Click
    xui.MsgboxAsync("Hello italy!", "B4X")
    Dim su As StringUtils
    Dim list1 As List

    list1 = su.LoadCSV(File.DirAssets, "Book2.csv", ",")

    Dim row() As String = list1.Get(1) 'second property in list
    row(1)="4333434" 'new meter reading
    For i = 0 To list1.Size-1
        Dim scol() As String = list1.Get(i)
   
        Dim NewRow As String = ""
        For Each item As String In scol
            NewRow = NewRow & " - " & item
        Next
        If NewRow.Length>3 Then  NewRow=NewRow.SubString(3)
        Log(NewRow)
    Next
End Sub

I would also check if the array is null or if the string values are null
 
Last edited:
Upvote 0

slaw54

Member
Thank you star dust The column items all vary in size(file attached but I was assuming that lists are dynamic arrays, I will try your code tomorrow, getting late for me in New Zealand, Thanks again.
 

Attachments

  • book2.txt
    407 bytes · Views: 84
Upvote 0

slaw54

Member
if the csv file was not uniform, i.e. each staff does not have an array of the same length, I would try this
B4X:
Sub Button1_Click
    xui.MsgboxAsync("Hello italy!", "B4X")
    Dim su As StringUtils
    Dim list1 As List

    list1 = su.LoadCSV(File.DirAssets, "Book2.csv", ",")

    Dim row() As String = list1.Get(1) 'second property in list
    row(1)="4333434" 'new meter reading
    For i = 0 To list1.Size-1
        Dim scol() As String = list1.Get(i)
  
        Dim NewRow As String = ""
        For Each item As String In scol
            NewRow = NewRow & " - " & item
        Next
        If NewRow.Length>3 Then  NewRow=NewRow.SubString(3)
        Log(NewRow)
    Next
End Sub

I would also check if the array is null or if the string values are null
Your code run without error, tomorrow I will try to work out why thanks for you assistance I have a long way to go but the forum is fantastic.
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Thank you star dust The column items all vary in size(file attached but I was assuming that lists are dynamic arrays, I will try your code tomorrow, getting late for me in New Zealand, Thanks again.
With the file you attached me, both codes work. Your code works fine too.

I tried to alter the csv file, specifically creating blank lines, a number of wrong separators etc .. But in none of the cases it generated an error

the error you reported indicates that you have tested access to element 0 (the first one) in an array with 0 elements. But the line you reported there is no access to items. Perhaps the line that generates the error is another?
 
Last edited:
Upvote 0

slaw54

Member
With the file you attached me, both codes work. Your code works fine too.

I tried to alter the csv file, specifically creating blank lines, a number of wrong separators etc .. But in none of the cases it generated an error

the error you reported indicates that you have tested access to element 0 (the first one) in an array with 0 elements. But the line you reported there is no access to items. Perhaps the line that generates the error is another?

I copied and pasted my own code back into a clean example and you are absolutely right it runs error free. I did not realize that the error logs are from the device not generated directly by the ide so it appears that the error was from a previous test. You live & learn. Appreciate your time and testing, Really impressed with the software and the Forum.
Cheers
 
Upvote 0
Top