B4J Question MySQL to TableView

Eumel

Active Member
Licensed User
Longtime User
Hi,

is it possible to put data from MySQL Table getting with JSON directly into a TableView from B4J ?

Based on the MySQL Example for B4A from Erel, i like to put the result in a TableView.

I tried it some ways, but the TableView looks empty. Only the Grid is shown.

I read the Tutorials, to get it work with DBUtils and SQLite. So i can put the result from JSON into SQLite, than with DBUtils into TableView.

But i look for a method, to save this step, und put the result directly into TableView.

If is needed, zip file attached. It´s a simple example...

____________
Eumel
 

Attachments

Code:
TableView1.Items.Clear
         TableView1.SetColumns(Array As String("id", "name"))
         For i = 0 To COUNTRIES.Size - 1
           Dim m As Map
           m = COUNTRIES.Get(i)
           'We are using a custom type named TwoLines (declared in Sub Globals).
           'It allows us to later get the two values when the user presses on an item.
           Dim tl As TwoLines
           tl.First = m.Get("id")
           tl.Second = m.Get("name")
           TableView1.Items
           TableView1.Items.Add(Array As Object(tl.First, tl.Second))
         Next
 
Upvote 0
Thanks Erel,

now i found what i done wrong ..
I thought it´s sufficient to declare the columns in designer.. After i add
TableView1.SetColumns(ArrayAsString("id", "name"))

my TableView show the result ..
 
Upvote 0
S.O.S

I'm doing a post based on this example with the following code:


Code:
Sub Process_Globals
    Private MainForm As Form
    Private b1 As Button
    Private t1 As TextField
    Type TwoLines (First As String, Second As String)
    Private b2 As Button
    Private COUNTRIES_LIST = "countries_list", COUNTRY_POPULATION = "country_population" As String
    Private TV1 As TableView
    Private TX1 As TextField
    Private TX2 As TextField
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Main") 'Load the layout file.
    MainForm.Show  
End Sub

Sub b1_Action
ExecuteRemoteQuery("SELECT NAME, PHONE FROM PHONES WHERE NAME LIKE'%" & t1.Text & "%' ORDER BY NAME" , COUNTRIES_LIST)
End Sub

Sub b2_Action
    MainForm.Close
End Sub

Sub ExecuteRemoteQuery(Query As String, JobName As String)
    Dim job As HttpJob
    job.Initialize(JobName, Me)
    job.PostString("http://www.xxxxx.com/xxxxxxxxxx.php", Query)  
End Sub

Sub JobDone(Job As HttpJob)
Dim res As String              
Dim parser As JSONParser

    If Job.Success Then  
        parser.Initialize(res)
      
        Select Job.JobName
            Case COUNTRIES_LIST
                Dim COUNTRIES As List
                COUNTRIES = parser.NextArray 'returns a list with maps
                TV1.SetColumns(Array As String("PHONE", "NAME"))
              
                For i = 0 To COUNTRIES.Size - 1
                    Dim m As Map
                    m = COUNTRIES.Get(i)
                    Dim tl As TwoLines
                    tl.First = m.Get("PHONE")
                    tl.Second = m.Get("NAME")
                    TV1.Items.Add(Array As Object(tl.First, tl.Second))
                Next

        End Select
    End If
    Job.Release
End Sub

Sub jobdone_ResponseSuccess (Response As HttpResponse, TaskId As Int)
    Dim res As String
    res = Response.GetString("UTF8")
End Sub


and is giving me the following error in the LOG window.

Code:
Program started.
main._jobdone (java line: 122)
org.json.JSONException: End of input at character 0 of
    at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
    at org.json.JSONTokener.nextValue(JSONTokener.java:97)
    at anywheresoftware.b4j.objects.collections.JSONParser.NextArray(JSONParser.java:58)
    at b4j.example.main._jobdone(main.java:122)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:425)
    at anywheresoftware.b4a.keywords.Common.access$0(Common.java:405)
    at anywheresoftware.b4a.keywords.Common$1.run(Common.java:461)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
    at java.lang.Thread.run(Thread.java:744)
 
Upvote 0
Could it be that 'res' in JobDone is not actually set to any parseable data? It appears to just be defined, no data moved into it.
(I have no knowledge of json parsing - so this is based on what I see from the code)
 
Upvote 0
Code:
        Select Job.JobName
            Case LISTADO
                Dim tmpList As List               
                Log ("a")               
                tmpList = parser.NextArray               
                Log ("b")
                TV1.Items.Clear
                TV1.SetColumns(Array As String("PHONE", "NAME"))     '<<<< ERROR           
                Log ("c")               
                For i = 0 To tmpList.Size - 1
                    Dim m As Map
                    m = tmpList.Get(i)
                    Dim tl As TwoLines
                    res = Job.GetString
                    tl.Primero = m.Get("TELEFONO")
                    tl.Segundo = m.Get("NOMBRE")
                    TV1.Items.Add(Array As Object(tl.Primero , tl.Segundo))
                Next
        End Select


Code:
Program started.

a
b
main._jobdone (java line: 129)
java.lang.RuntimeException: Object should first be initialized (TableView).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:28)
    at anywheresoftware.b4j.objects.TableViewWrapper.getItems(TableViewWrapper.java:153)
    at b4j.example.main._jobdone(main.java:129)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:425)
    at anywheresoftware.b4a.keywords.Common.access$0(Common.java:405)
    at anywheresoftware.b4a.keywords.Common$1.run(Common.java:461)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
    at java.lang.Thread.run(Thread.java:744)
 
Upvote 0
It needs to go before you initialize the json.parser
change
Code:
Sub JobDone(Job As HttpJob)
Dim res As String
Dim parser As JSONParser
to
Code:
Sub JobDone(Job As HttpJob)
Dim res As String = Job.GetString
Dim parser As JSONParser
 
Upvote 0
It's been a while of this, but someone manage to get it?
I´m Getting the string information right, but there is no way to put in a table....
 
Upvote 0
Nevermind.... i just made it...

For the people that is fighting.... This WORKS 100%..I need to do some Weird stuff, still figure it out why... for example, i have to unchecked the lib http and put it as a bas (don't know why). when i Load the bas the system tells me that it's done for a newer version (?)...only said this once and that's it
Also, if i don't Clear and define columns of the listviews, doesn't work...

I'm still figure it out those things, but is a start.

Best regards
 

Attachments

Last edited:
Upvote 0
Back
Top