Can't seem to get my ListView to show

jtaj

Member
Licensed User
Longtime User
I hope I am posting this correctly. I am a B4A newbie and I'm sure it is something simple.

I have a layout with a listview that I am populating with a sqlite call. The SQL call works, I see the expected log messages. But the screen never update my listview with the new values. PopulateList gets called each time a spinner value is selected and a database insert is complete. So the listview is supposed to show the updated list to include the newest record.

What am I doing wrong?

B4X:
Sub PopulateList()
   Dim cur As Cursor
   Dim sqlcmd As String
   Dim linetext As String
      
   sqlcmd = "SELECT col1, col2, col3 FROM table "

   cur = Main.SQL.ExecQuery(sqlcmd)
   For i = 0 To cur.RowCount - 1
      cur.Position = i
      linetext = cur.GetString("col1") & " : " & cur.GetString("col2")
      ListView1.AddSingleLine(linetext)
      Log("data=" & linetext)
   Next
   Log("all done")
End Sub
 

mc73

Well-Known Member
Licensed User
Longtime User
Perhaps you should try moving your
B4X:
dim lineText as string
line, inside the loop. Another thing irrelevant that I noticed, is that you don't close the cursor after your loop ends.
 
Upvote 0

jtaj

Member
Licensed User
Longtime User
Thanks for responding so quickly. Yes, I am initializing the listview at the start of the activity.

The other post was to put the linetext inside the loop, but that isn't the problem, the linetext is spit out to the log appropriately.


I was trying to simplify my code before, but here is what I have line for line.
B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim btnDiscount As Button
   Dim btnDonation As Button
   Dim lblDtTm As Label
   Dim lblSalesId As Label
   Dim lbl1 As Label
   Dim lbl2 As Label
   Dim lblTotal As Label
        Dim lblcount as Label
   Dim SpnUpc As Spinner
   Dim lbldesc As Label
   Dim lblprice As Label
   Dim lblReduce As Label
   Dim lblsnum As Label
   Dim btnFindSale As Button
   Dim Label1 As Label
   Dim Label2 As Label
   Dim pnlItemDet As Panel
   
   Dim v_reduced As String
   Dim v_price As Float
   Dim v_total As Float
   Dim v_count As Int
      
   Dim ListView1 As ListView
End Sub

'**********New Sale is created, waiting on items to be added ******
Sub New()
   Dim dttm As String
   Dim now As Long
   Dim rowid As String
   Dim sqlcmd As String
   
   DateTime.TimeFormat = "HH:mm"
   now = DateTime.now
   dttm = DateTime.Date(now) & " " & DateTime.Time(now)
   lblDtTm.Text = dttm
   sqlcmd = "INSERT INTO sales VALUES(Null, 'mobile','teresa','" & dttm & "','" & dttm & "','','',0);"
   Main.SQL.ExecNonQuery(sqlcmd)
   rowid = Main.SQL.ExecQuerySingleResult("SELECT ROWID from sales order by ROWID DESC limit 1")
   Log("Last Sale ID inserted=" & rowid)
   lblSalesId.Text = rowid
End Sub

'*********************Activity Create ***************
Sub Activity_Create(FirstTime As Boolean)
   v_total = 0
   Activity.LoadLayout("Sales")
   New  ' creates a new master record
   If FirstTime Then
      ListView1.Initialize("ListView1")
      ' Fill my spinner.  When a value is chosen on the spinner, trigger the on click event
      DBUtils.ExecuteSpinner(Main.SQL, "select upc from items_checkin order by upc",Null,0 , SpnUpc)
   End If
End Sub

'*******************PopulateList of items added *****************
Sub ShopCartLoad()
   Dim cur As Cursor
   Dim sqlcmd As String
   Dim linetext As String
      
   sqlcmd = "SELECT items_sold.upc as isupc, description, items_sold.price as isprice FROM items_sold, items_checkin WHERE items_checkin.upc = items_sold.upc and items_sold.sales_id=" & lblSalesId.Text
   'DBUtils.ExecuteListView(Main.SQL, sqlcmd, Null, 200, lv1, False)
   
   cur = Main.SQL.ExecQuery(sqlcmd)
   For i = 0 To cur.RowCount - 1
      cur.Position = i
      linetext = cur.GetString("isupc") & " : " & cur.GetString("description")& " " &cur.GetString("isprice")
      ListView1.AddSingleLine(linetext)
   Next
End Sub

'*********************UPC Spinner onclick  ***************
Sub SpnUpc_ItemClick (Position As Int, Value As Object)
   Dim Send As Spinner            
   Dim upc As String
   Dim cur As Cursor
   Dim sqlcmd As String
   Dim results As Map

   Send = Sender   
   
   upc = Send.GetItem(Position)
   sqlcmd = "select description,price,seller_number,reduce from items_checkin where upc = '" & upc & "'"
   results = DBUtils.ExecuteMap(Main.SQL,sqlcmd,Null)
   
   'Build the labels on the screen as formatted for viewing.  the v_ variables will be used for database insertion
   lblprice.Text = "$" & NumberFormat2(results.Get("price"), 0,2,2,True)
   v_price = results.Get("price")
   lbldesc.text = results.Get("description")
   lblsnum.text = results.Get("seller_number")
   If (results.Get("reduce")= 1) Then
      lblReduce.Text = "Y"
      v_reduced = "reduced"
   Else
      lblReduce.Text = "N"
      v_reduced = ""
   End If
   
   v_total = Main.SQL.ExecQuerySingleResult("select sum(price) from items_sold where sales_id = " & lblSalesId.Text)
   lblTotal.Text = "$" & NumberFormat2(v_total, 0,2,2,True)
   v_count = Main.SQL.ExecQuerySingleResult("select count(*) from items_sold where sales_id = " & lblSalesId.Text)
   lblcount.Text = v_count
   
   BuildItemsSoldMap(Position)
   ShopCartLoad
   
End Sub
 
Last edited:
Upvote 0

jtaj

Member
Licensed User
Longtime User
Yeah it works now

:sign0060:
I closed the cursor like mc73 said to and I deleted the ListView.initialize out of my activity create sub.

If you happen to see anything overtly wrong with my code, please let me know. Although I've been programming what seems like my whole life, new languages and platforms are always a challenge....and fun too!

It is working. :) One hurdle overcome!

Blessings,
Teresa
 
Upvote 0
Top