B4J Tutorial Customized ListView

rwblinn

Well-Known Member
Licensed User
Hi,

the item added to the listview is an anchorpane which contains a label. To get the text of the label, use

B4X:
Dim ap As AnchorPane = lv.SelectedItem
Dim lbl1 As Label = ap.GetNode(0)
Log(lbl1.Text)

This is an example of how to add an anchorpane with a label
B4X:
Dim ap As AnchorPane
ap.Initialize("")
Dim lbl1 As Label
lbl1.Initialize("")
lbl1.Text = "Hello World"                  
lbl1.Font = fx.DefaultFont(16)
ap.AddNode(lbl1, 0, 0, lv.Width, 20dip)
lv.Items.Add(ap)
 

StarinschiAndrei

Active Member
Licensed User
Hi,

the item added to the listview is an anchorpane which contains a label. To get the text of the label, use

B4X:
Dim ap As AnchorPane = lv.SelectedItem
Dim lbl1 As Label = ap.GetNode(0)
Log(lbl1.Text)
Program started.
11
Error occurred on line: 72 (main).
java.lang.ClassCastException: javafx.scene.layout.AnchorPane cannot be cast to javafx.scene.control.Label
at b4j.example.main._lv_selectedindexchanged(main.java:164)
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:601)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:563)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:221)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:156)
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:601)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:82)
at anywheresoftware.b4a.BA$2.run(BA.java:165)
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.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
at java.lang.Thread.run(Thread.java:722)


This is an example of how to add an anchorpane with a label
B4X:
Dim ap As AnchorPane
ap.Initialize("")
Dim lbl1 As Label
lbl1.Initialize("")
lbl1.Text = "Hello World"                 
lbl1.Font = fx.DefaultFont(16)
ap.AddNode(lbl1, 0, 0, lv.Width, 20dip)
lv.Items.Add(ap)
With this it work fine.
Thank you
 

TomDuncan

Active Member
Licensed User
With the images example can I select more than 1 image, Then after get all images selected.
Nice to have a highlight for selected.
1 click to select then click on it again to unselect. (well a thought)

Tom
 

rwblinn

Well-Known Member
Licensed User
Hi Tom,

one way can think of is:

Set the Selection Mode of the Listview to MULTIPLE
B4X:
'Set the selection mode of the listview
'Parameter: Listview, Selectionmode with value SINGLE or MULTIPLE - Exact case to be used
'Example: ListviewSetSelectionMode(ListView1, "MULTIPLE")
Sub ListviewSetSelectionMode(lvw As ListView, selectionmode As String)
    Dim joListView As JavaObject = lvw
    Dim joSelectionMode As JavaObject
    joSelectionMode.InitializeStatic("javafx.scene.control.SelectionMode")
    joListView.RunMethodJO("getSelectionModel", Null).RunMethod("setSelectionMode", Array(joSelectionMode.RunMethod("valueOf", Array(selectionmode))))
End Sub

Select the Listview Indices
B4X:
Dim l As List = ListView1.GetSelectedIndices
Log("Items Total: " & ListView1.Items.Size & ", Selected: " & l.size)
For i = 0 To l.Size - 1
  Log("Selected Index: " & l.Get(i))
Next
 
Top