Loop through listview to display mySql

grant1842

Active Member
Licensed User
Longtime User
I am getting strings from 3 dirrernt fields from a mySql database.
It works good enough just to get 1 row. I need to display all of the (comments) field in a Listview.

Here is mySql Code.
B4X:
if ( isset($_GET['action']) && $_GET['action'] == "get_scores" ) {
   $query_scores = "SELECT home,opp,comments FROM mysqlscore";
   $result_scores = mysql_query($query_scores) or die(mysql_error());
   $row_scores = mysql_fetch_assoc($result_scores);
   $home = $row_scores['home'];
   $opp = $row_scores['opp'];
   $comments = $row_scores['comments'];
   echo $home.','.$opp.'-'.$comments;

I am trying to fetch all the fields in a table and display (home,opp) in 2 textedits, and comments in a litview.


Here is B4A code that i am trying to figure out how to loop through the listview and add the mySql field (comments) into.
B4X:
 Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
   Dim res As String
   Dim home As String
   Dim opp As String
   Dim comments As String
   Dim listcount As Int
   Dim listvalue As String
   res = Response.GetString("UTF8")
   Log("Response from server: " & res)
   
   home = res.SubString2(0, res.IndexOf(","))
   
   opp = res.SubString2(res.IndexOf(",")+1, res.IndexOf("-"))
   
   
   
    comments = res.SubString2(res.IndexOf("-")+1, res.Length())
   Label2.Text = home
   Label1.Text = opp
   
    listcount = 1

   For I = 1 To listcount
    ListView1.AddSingleLine(comments)
   Next
   
   Response.Release
   
   
End Sub

This just shows 1 row of the Field (comments)


Any help and comments are appreciated.
Thanks for your time and help.
 
Last edited:

grant1842

Active Member
Licensed User
Longtime User
@netchicken this is great training and learning material . (GREAT JOB).
I am just starting out in this.

I have added 2 fields into the database (home) and (opponent) as int.

I have added 2 labels(txtHome),(txtOpp) to the pannel.

I am trying to figure out how to use the reader.readline to get the lines i need.

Here is my code.
B4X:
Sub httpC_ResponseSuccess (Response As HttpResponse, TaskId As Int)
      lvoutput.Clear 'clear the List box so it loads the new data in fresh
      Dim result As String 'download results
      result = Response.GetString("UTF8")
      
      'strip out the underscores
      result = result.Replace("_"," ")
      'write it to a file
      File.WriteString(File.DirInternalCache, "nettalk.txt",result)
      'get it back from the file
      reader.Initialize(File.OpenInput(File.DirInternalCache, "nettalk.txt"))
      
      'read it line by line to a string called line,
      Dim name ,home, opponent As String
      name = reader.ReadLine 'this loads the username "me" echo $row['name'];
      Dim i As Int :i = 0'dimension a counter just to show in the scrollview
      Do While name <> Null 'run a loop while there is a line
      i = i+1
      'returns the message text echo "\r\n".$row['text'];
      Dim linetext As String
      
      linetext = reader.ReadLine
      'returns the DeviceID text echo "\r\n".$row['text'];
      home = reader.ReadLine
      opponent = reader.ReadLine
      
      Dim postdeviceID As String
      postdeviceID = reader.ReadLine
      
      txtHome.Text = (home)
      txtOpp.Text = (opponent)
      
      'Set up the listview formatting and adds name and then text
      lvoutput.SingleLineLayout.Label.TextSize = 16
      lvoutput.SingleLineLayout.ItemHeight = 35
      lvoutput.SingleLineLayout.Label.TextColor = Colors.Black
      lvoutput.AddSingleLine(i&" "& name & " "& linetext)
      
      name = reader.ReadLine 'passes only to the ME text
      Loop
      
      reader.Close
      ProgressDialogHide 'stop the progress dialog box
      lvoutput.SetSelection(0)
      End Sub
When i press the Read all it shows the wrong data in the labels, and error.
java.lang.NullPointerException .

If you can show me what I am doing wronge in reading the lines and why I would really appreciate it. Thanks for your time.


I modified the php.
B4X:
<?php
HIDDEN:)

if(isset($_GET['action'])){
$action = $_GET['action'];
}
if($action == "add"){
$name = $_GET['name'];
$deviceID = $_GET['deviceID'];
$text = $_GET['text'];
$home = $_GET['home'];
$opponent = $_GET['opponent'];
mysql_query("INSERT INTO netdb
( name, deviceid, text,home, opponent) VALUES( '$name', '$deviceID', '$text','$home', 
'$opponent') ")
or die(mysql_error());
}
//if($action == "getall"){
//$result = mysql_query("SELECT name, text, deviceID FROM netdb" ) or die(mysql_error());
if($action == "getall"){
$result = mysql_query("SELECT ID, name, text, deviceID, home, opponent FROM netdb
ORDER BY TIMESTAMP DESC LIMIT 10 " ) or die(mysql_error());

//DATA OUT TO PROGRAM
while($row = mysql_fetch_array($result)){

echo $row['ID'];
echo $row['name'];
echo "\r\n".$row['text'];
echo "\r\n".$row['deviceID']."\r\n";
echo $row['home'];
echo $row['opponent'];
echo $name;
echo $deviceID;
echo $text;
echo $home;
echo $opponent;
}
}

//delete messages
if($action == "delete"){
$deleteID = $_GET['ID'];
mysql_query("DELETE FROM netdb WHERE ID = '$deleteID'" ) or die(mysql_error());
}
?>

Thanks
 
Upvote 0
Top