Hi
Mashy's B4J Overview
I'm new to b4j and have just finished my project in it which is a helper for me in relation to php generation. I shared my creation here. As I am working on an updated version, I needed some functionality on the listview,e.g. returning all items as a map, search for a text or searching for a tag property. Whilst this will start with code on a listview, I intend to collect my snippets here.
Mashy's B4J Overview
I'm new to b4j and have just finished my project in it which is a helper for me in relation to php generation. I shared my creation here. As I am working on an updated version, I needed some functionality on the listview,e.g. returning all items as a map, search for a text or searching for a tag property. Whilst this will start with code on a listview, I intend to collect my snippets here.
B4X:
'Description: add two lines to a listview
'Tag: listview, add two lines
Sub ListViewAddTwoLines(lv As ListView, Line1 As String, Line2 As String, Value As Object)
Dim ap As AnchorPane
ap.Initialize("")
Dim lbl1, lbl2 As Label
lbl1.Initialize("")
lbl1.Text = Line1
lbl1.Font = fx.DefaultFont(14)
lbl2.Initialize("")
lbl2.Text = Line2
lbl2.Font = fx.DefaultFont(12)
ap.AddNode(lbl1, 0, 0, lv.Width, 20dip)
ap.AddNode(lbl2, 0, 25dip, lv.Width, 20dip)
lv.Items.Add(ap)
lbl1.Tag = Value
End Sub
B4X:
'Description: add a single line to a listview
'Tag: listview, add single line
Sub ListViewAddOneLine(lv As ListView, Line1 As String, Value As Object)
Dim ap As AnchorPane
ap.Initialize("")
Dim lbl1 As Label
lbl1.Initialize("")
lbl1.Text = Line1
lbl1.Font = fx.DefaultFont(14)
lbl1.Tag = Value
ap.AddNode(lbl1, 0, 0, lv.Width, 20dip)
lv.Items.Add(ap)
End Sub
B4X:
'Description: search for tag from a listview and return boolean
'Tag: b4j, listview, text, search
Sub ListViewTagExist(lstView As ListView, searchTag As String) As Boolean
Dim l As List
Dim i As Int
Dim t As Int
Dim m As Map
Dim txt As String
searchTag = searchTag.ToLowerCase
' get all items as a list
l = ListViewGetItems(lstView)
t = l.Size - 1
For i = 0 To t
m = l.Get(i)
txt = m.Get("tag")
txt = txt.ToLowerCase
If txt = searchTag Then
Return True
End If
Next
Return False
End Sub
B4X:
'Description: search for text from a listview and return boolean
'Tag: b4j, listview, text, search
Sub ListViewTextExist(lstView As ListView, searchText As String) As Boolean
Dim l As List
Dim i As Int
Dim t As Int
Dim m As Map
Dim txt As String
searchText = searchText.ToLowerCase
' get all items as a list
l = ListViewGetItems(lstView)
t = l.Size - 1
For i = 0 To t
m = l.Get(i)
txt = m.Get("text")
txt = txt.ToLowerCase
If txt = searchText Then
Return True
End If
Next
Return False
End Sub
B4X:
'Description: return all items in the listview as a list of maps
'Tag: listview, b4j, map, anchorpane
Sub ListViewGetItems(lstView As ListView) As List
Dim lstTarget As List
Dim ap As AnchorPane
Dim title As Label
Dim m As Map
Dim l As List
l.Initialize
' get all the items from the list
lstTarget = lstView.items
' loop through each item
For I = 0 To lstTarget.Size - 1
ap = lstTarget.Get(I)
title = ap.GetNode(0)
m.Initialize
m.Put("tag", title.tag)
m.Put("text", title.Text)
l.Add(m)
Next
Return l
End Sub
B4X:
'Description: return a tag and text of selected listview item as a map
'Tag: listview, map, tag, text
Sub ListViewGetSelected(lstView As ListView) As Map
Dim m As Map
m.Initialize
Dim fsel As Int = lstView.SelectedIndex
If fsel = -1 Then
m.Put("tag", "")
m.Put("text", "")
m.Put("index","-1")
Else
' get the selected item
Dim ap As AnchorPane = lstView.SelectedItem
Dim title As Label = ap.GetNode(0)
m.Put("tag", title.tag)
m.Put("text", title.Text)
m.Put("index", fsel)
End If
Return m
End Sub
Last edited: