Android Question KeyValueStore2 B4XTable1.SetData with strings

Rusty

Well-Known Member
Licensed User
I have a list of strings stored in a KVS. It was written with one program and I am trying to read it with another. The strings are all like "item1", "Item2"...
B4X:
    Commands.Contents = kvs.Get(1)
 
    Dim data As List
    data.Initialize
    For i = 0 To Commands.Contents.ItemName.Size -1
        Log(i & " - " & Commands.Contents.ItemName.Get(i))
        data.Add(Commands.Contents.ItemName.Get(i))  
        'note, Commands.Contents.ItemName is a list
        'I load the contents into another list only
        'because I got frustrated and tried this
        'I think it should work with the KVS list...
        'BTW, the LOGs show the correct content of the stored list...
    Next
    B4XTable1.AddColumn("Item", B4XTable1.COLUMN_TYPE_TEXT)
    B4XTable1.SetData(data)
When I hit the SetData line above it abends with:
Copying updated assets files (9)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Error occurred on line: 571 (B4XTable)
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Object[]
at b4a.example.b4xtable$ResumableSub_SetData.resume(b4xtable.java:346)
at b4a.example.b4xtable._setdata(b4xtable.java:213)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
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 b4a.example.main.afterFirstLayout(main.java:104)
at b4a.example.main.access$000(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:82)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:208)
at android.app.ActivityThread.main(ActivityThread.java:6304)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
Any insights will be appreciated, including a better way :)
Rusty
 
Last edited:

Rusty

Well-Known Member
Licensed User
Hi Don,
Good question… B4XTable is library from Anywhere (B4a, B4J...etc. compatible). Found HERE
I don't have source code for this...
Rusty
 

DonManfred

Expert
Licensed User
I don't have source code for this...
You are filling a List with sinple strings, right?
But if i remember correctly each item in the List should be an ARRAY of Objects (for each column one String). Not just ONE String.

B4X:
Dim Data As List
   Data.Initialize
   Dim rs As ResultSet = sql.ExecQuery("SELECT CustomerId, FirstName, LastName, Company, Address FROM customers")
   Do While rs.NextRow
       Dim row(4) As Object ' ATTENTION... Array
       row(0) = rs.GetDouble("CustomerId")
       row(1) = rs.GetString("FirstName") & " " & rs.GetString("LastName")
       row(2) = rs.GetString("Company")
       'Some of the values are Null. We need to convert them to empty strings:
       If row(2) = Null Then row(2) = ""
       row(3) = rs.GetString("Address")
       Data.Add(row) ' Adding an Array, not a String
   Loop
   rs.Close
   B4XTable1.SetData(Data)
Note that i saw the Lib but did not used it as of now.
 
Last edited:
Top