Android Question (SOLVED)List Can't be used in a BXTable

Daniel44

Active Member
Licensed User
Helo everyone!

I have a Standart Class Called Productos like this:
B4X:
Public Sub SelectAll(Filtro As String) As List
    Dim Cursor As Cursor
    Dim Data As List
    Data.Initialize()
    
    Cursor = Connection.mySQL.ExecQuery2("SELECT * FROM productos WHERE NombreProd LIKE ?", Array As String("%" & Filtro & "%"))


    For i = 0 To Cursor.RowCount -1 Step 1
        Cursor.Position = i
        
        Dim Record As Producto
        Record.Id = Cursor.GetInt("IdProd")
        Record.Nombre = Cursor.GetString("NombreProd")
        Record.Precio = Cursor.GetDouble("PrecioProd")
        Record.Stock = Cursor.GetInt("Stock")
        Data.Add(Record)
    Next
    Return Data
End Sub
This Class uses another Standart Class called Producto like this:

B4X:
Sub Class_Globals
    
    Public Id As Int
    Public Nombre As String
    Public Precio As Double
    Public Stock As Int
    
    
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    
End Sub
Next I've instantiated the Productos Class in my Main Activity in order to use the list in my BXTable but... It doesn't work: I've done this in my Main Activity:

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim Prods As Productos
    
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("mainly")
    If FirstTime Then
        CopyDBIfNeeded("bdtest.db")
        Connection.mySQL.Initialize(File.DirInternal, "bdtest.db", False)
    End If
    
    Prods.Initialize
    MostrarProductos
End Sub

Sub MostrarProductos
    Dim listaDatos As List = Array As Object(Prods.SelectAll(""))
    Log(listaDatos)
        
    
    TBLProductos.AddColumn("ID",TBLProductos.COLUMN_TYPE_NUMBERS).Width = 20
    TBLProductos.AddColumn("NOMBRE PROD", TBLProductos.COLUMN_TYPE_TEXT)
        
    NumberColumn =TBLProductos.AddColumn("PRECIO", TBLProductos.COLUMN_TYPE_NUMBERS)
    CreateCustomFormat(NumberColumn)
TBLProductos.AddColumn("STOCK ", TBLProductos.COLUMN_TYPE_NUMBERS)

Dim Row As Producto
Dim Lista As List
Lista.Initialize

Row.Id = listaDatos.Get("id")
Row.Nombre = listaDatos.Get("nombre")
Row.Precio = listaDatos.Get("precio")
Row.Stock = listaDatos.Get("stock")

Lista.Add(Row)

TBLProductos.SetData(Lista)
End Sub
But the app crashes "error on line 97(Main)
java.lang.NumberFormatException: For input String "id"

I'v changed this and I tried with a For each on listaDatos but it crashes. The log (Log(listaDatos) is retreiving data:

B4X:
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
(ArrayList) [[[connection=null, dateutils=null, dbutils=null
, edproductos=null, id=1, main=null
, nombre=Toy1, precio=33.0, starter=null
, stock=100], [connection=null, dateutils=null, dbutils=null
, edproductos=null, id=2, main=null
, nombre=Toy2, precio=44.44, starter=null
, stock=89]]]
Error occurred on line: 97 (Main)
java.lang.NumberFormatException: For input string: "id"
I've tried with a For Next as well but it's still crashing . I don't know what I'm doing wrong. If you notice the Public Sub SelectAll(without Filter in this case) is returning a List so I can't understand
why it doesn't work for my BXTable.

I've changed it putting a Resultset but it crashes saying it can not cast the list with a cursor, and putting a Do While on it.. nothing seems work

I'm working with DBUtils and a Sqlite Database

Thank you
 

Unobtainius

Active Member
Licensed User
I would try only using the WHERE if your filter contains something.
 

Daniel44

Active Member
Licensed User
B4XTable.SetData expects a list where each item is an array of objects.
B4X:
Dim Row(4) As Object
Dim Lista As List
Lista.Initialize

Row(0) = listaDatos.Get("id")
Row(1) = listaDatos.Get("nombre")
Row(2) = listaDatos.Get("precio")
Row(3) = listaDatos.Get("stock")

Lista.Add(Row)

TBLProductos.SetData(Lista)
Thank you Erel but it's still crashing
B4X:
Sub MostrarProductos
    Dim listaDatos As List = Array As Object(Prods.SeleccionarTodo(""))
    Log(listaDatos)
'      

    TBLProductos.AddColumn("ID",TBLProductos.COLUMN_TYPE_NUMBERS).Width = 20
    TBLProductos.AddColumn("NOMBRE PROD", TBLProductos.COLUMN_TYPE_TEXT)
       
   
    NumberColumn =TBLProductos.AddColumn("PRECIO", TBLProductos.COLUMN_TYPE_NUMBERS)
    CreateCustomFormat(NumberColumn)
    TBLProductos.AddColumn("STOCK ", TBLProductos.COLUMN_TYPE_NUMBERS)
   
   
    Dim Row(4) As Object
   
    Dim Lista As List
    Lista.Initialize
   
    Row(0) = listaDatos.Get("id")
    Row(1) = listaDatos.Get("nombre")
    Row(2) = listaDatos.Get("precio")
    Row(3) = listaDatos.Get("stock")
   
    Lista.Add(Row)
    TBLProductos.SetData(Lista)
End Sub

--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
(ArrayList) [[[connection=null, dateutils=null, dbutils=null
, edproductos=null, id=1, main=null
, nombre=Toy1, precio=33.0, starter=null
, stock=100], [connection=null, dateutils=null, dbutils=null
, edproductos=null, id=2, main=null
, nombre=Toy2, precio=44.44, starter=null
, stock=89]]]
Error occurred on line: 107 (Main)
java.lang.NumberFormatException: For input string: "id"
I've created some variables with its type ex: ID As Int etc but it doesn't work either
Thank you
 

Daniel44

Active Member
Licensed User
I fixed it. Thank you all

B4X:
Sub MostrarProductos       
        TBLProductos.AddColumn("ID",TBLProductos.COLUMN_TYPE_NUMBERS).Width = 20
    TBLProductos.AddColumn("NOMBRE PROD", TBLProductos.COLUMN_TYPE_TEXT)
        
    
    NumberColumn =TBLProductos.AddColumn("PRECIO", TBLProductos.COLUMN_TYPE_NUMBERS)
    CreateCustomFormat(NumberColumn)
    TBLProductos.AddColumn("STOCK ", TBLProductos.COLUMN_TYPE_NUMBERS)
    
    
    Dim Lista As List
    Lista.Initialize
    Dim LS As List
       
    Dim R As Producto
    LS = Prods.SeleccionarTodo("")
    
    For i = 0 To LS.Size -1
        R = LS.Get(i)
        
        Dim Row(4) As Object
        Row(0) = R.Id
        Row(1) = R.Nombre
        Row(2) = R.Precio
        Row(3) = R.Stock
        Lista.Add(Row)
    Next
    TBLProductos.SetData(Lista)
End Sub
 
Top