Android Question Problem with String Utils save CSV file

gezueb

Active Member
Licensed User
Longtime User
Hi, my app collects values from the user, adds them to a history list and a history table (table module) and then should save the table. However, the save crashes with a java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[]

Here is the relevant portion of the code
B4X:
    Dim historyListitem As Object = Array (Datum,spnHour.SelectedItem,spnMinute.SelectedItem,txtSys.Text,txtDia.Text,txtPuls.Text,timestamp)
    historyList.Add (historyListitem)
    historyTable.ClearAll 'table from the table module
    For i = 0 To historyList.Size-1 'discard the header
        Dim hti() As Object = Array(7) 'hti = history list item
        hti = historyList.Get(i)
        historyTable.AddRow(Array As String(hti(0),hti(1),hti(2),hti(3),hti(4),hti(5),hti(6))) 'works
    Next
    'now write back the table to the csv file
    SU.SaveCSV(File.DirInternal,"history.csv",",",historyList)
    'crashes with java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[]

timestamp is a long, all the other arguments are strings. However, if I change timestamp to string, same problem.

Help much appreciated!
 

William Lancee

Well-Known Member
Licensed User
Longtime User
The SU.SaveCSV routine expects a very specific list format.
All items have to be String Arrays of the same length.

In your case, historyListitem should be a string array, not an object array.
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
I changed the declaration to
B4X:
    Dim historyListitem = Array As String (Datum,spnHour.SelectedItem,spnMinute.SelectedItem,txtSys.Text,txtDia.Text,txtPuls.Text,timestamp)
however, the exception persists.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Maybe
B4X:
Dim historyListitem() As Object = Array As String(Datum,spnHour.SelectedItem,spnMinute.SelectedItem,txtSys.Text,txtDia.Text,txtPuls.Text,timestamp)
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
Yes indeed,, now it works! Thanks a lot, guys! Georg
B4X:
    Dim historyListitem() As Object = Array As String(Datum,spnHour.SelectedItem,spnMinute.SelectedItem,txtSys.Text,txtDia.Text,txtPuls.Text,timestamp)
    historyList.Add (historyListitem)
    historyTable.ClearAll
    For i = 0 To historyList.Size-1 'discard the header
        Dim hti() As Object = Array (7) 'history table item
        hti = historyList.Get(i)
        historyTable.AddRow(Array As String(hti(0),hti(1),hti(2),hti(3),hti(4),hti(5),hti(6))) 'works
    Next
    'now write back the table to the csv file
    SU.SaveCSV(File.DirInternal,"history.csv",",",historyList)
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
You could probably simplify your code too (but I can't test)

B4X:
    Dim historyListitem() As Object = Array As String(Datum,spnHour.SelectedItem,spnMinute.SelectedItem,txtSys.Text,txtDia.Text,txtPuls.Text,timestamp)
    historyList.Add (historyListitem)
    historyTable.ClearAll
    For i = 0 To historyList.Size-1 'discard the header
        historyTable.Add(historyList.get(i))
    Next
    'now write back the table to the csv file
    SU.SaveCSV(File.DirInternal,"history.csv",",",historyList)
 
Upvote 0

gezueb

Active Member
Licensed User
Longtime User
Indeed, this is simpler:
B4X:
    For i = 0 To historyList.Size-1 'discard the header
        historyTable.Add[B]Row[/B](historyList.get(i))
    Next
The list item is not necessary.
Thanks to all of you!
 
Upvote 0
Top