Android Question Not working At B4x Table

Pravee7094

Active Member
Hello all,

I try to list the data from the postgres database using B4x Table. But I got some Errors.
But when I list the same data using listview Its perfectly works. What else I miss? I attached the Two type of codes.

List data using B4X Table : (Not Working)

B4X:
Sub PrintTable
    printDataTable.AddColumn("Name", printDataTable.COLUMN_TYPE_TEXT)
    printDataTable.AddColumn("Email", printDataTable.COLUMN_TYPE_TEXT)
    printDataTable.AddColumn("City", printDataTable.COLUMN_TYPE_TEXT)
    printDataTable.AddColumn("Distirct", printDataTable.COLUMN_TYPE_TEXT)
    Dim Connect As HttpJob
    Connect.Initialize("PrintData", Me)
    Connect.Download(Main.strURL & "print_data.php")
End Sub


Sub JobDone (Job As HttpJob)
    ProgressDialogHide
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then
        Select Job.JobName
        
        Case "PrintData"
            Dim res As String
            res = Job.GetString
            Log(res)
            Dim parser As JSONParser
            parser.Initialize(res)
            Dim ListAllItems As List
            ListAllItems.Initialize
                    
                ListAllItems = parser.NextArray 'returns a list with maps
                    
                    For i = 0 To ListAllItems.Size - 1
                        Dim Person As Map
                        Person = ListAllItems.Get(i)
                        Log(Person)
                        Dim row() As Object
                        row(0) = Person.Get("name")
                        row(1) = Person.Get("email")
                        row(2) = Person.Get("city")
                        row(3) = Person.Get("distirct")
                        ListAllItems.Add(row)
                    Next
                    printDataTable.SetData(ListAllItems)
            End Select
    Else
        Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub

PHP code:
PHP:
<?php
$dbconn = pg_connect("host=localhost port=5433 dbname=dbname user=postgres password=password");

    $q = pg_query("SELECT name,email,city,distirct FROM product_item ORDER BY name ASC");
    $rows = array();
    while($r = pg_fetch_assoc($q))
      {
        $rows[] = $r;
      }
        print json_encode($rows);
?>

Here, List data using ListView: (working)

B4X:
Sub ListTable
    Dim Connect As HttpJob
    Connect.Initialize("PrintData", Me)
    Connect.Download(Main.strURL & "print_data.php")
End Sub


Sub JobDone (Job As HttpJob)
    ProgressDialogHide
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then
        Select Job.JobName
        
        Case "PrintData"
            Dim res As String
            res = Job.GetString
            Log(res)
            Dim parser As JSONParser
            parser.Initialize(res)
            Dim ListAllItems As List
            ListAllItems.Initialize
            Dim personName As String
                    
                ListAllItems = parser.NextArray 'returns a list with maps
                    
                    For i = 0 To ListAllItems.Size - 1
                        Dim person As Map
                        person = ListAllItems.Get(i)
                        Log(person)
                        personName = person.Get("name")
                    lstViewItems.AddSingleLine(personName)
                    Next

PHP code same php code)
PHP:
<?php
$dbconn = pg_connect("host=localhost port=5433 dbname=dbname user=postgres password=password");

    $q = pg_query("SELECT name,email,city,distirct FROM product_item ORDER BY name ASC");
    $rows = array();
    while($r = pg_fetch_assoc($q))
      {
        $rows[] = $r;
      }
        print json_encode($rows);
?>

Any body help? Or any other code snippets are there?

Thanks
Praveen
 

Mahares

Expert
Licensed User
Longtime User
Any body help? Or any other code snippets are there?
I think your problem is in this line: printDataTable.SetData(ListAllItems)
You are extracting the list of maps from the jason into ListAllItems list and trying to put back the same list in the B4XTable. You cannot do that. You need something like this:

B4X:
dim l as list
l.initialize
ListAllItems = parser.NextArray 'returns a list with maps              
                    For i = 0 To ListAllItems.Size - 1
                        Dim Person As Map
                        Person = ListAllItems.Get(i)
                        Dim row() As Object
                        row(0) = Person.Get("name")
                        row(1) = Person.Get("email")
                        row(2) = Person.Get("city")
                        row(3) = Person.Get("distirct")
                        l.Add(row)
                    Next
                    printDataTable.SetData(l)
Also, as a side note, nothing to do with your code, but you need to change: distirct to district for proper english.
 
Upvote 0

Pravee7094

Active Member
@Mahares Thanks for your suggestion. Its perfectly working. You made my day

I know the wrong spell (distirct). I accidentally typo in database. So don't worry.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…