B4J Question Listview

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi all

I have brought across a Sub from B4A and attempted to convert it to B4J. I have failed. I have followed Erels B4J Tutorial, but I couldn't always relate it to my sub.

The code is below, any hints appreciated.

Regards Roger

B4X:
Sub FileSelect
'Brings up ListView to allow selection of files.
'Called by BtnFiles_Action Sub
    Dim temp As String
    ImportFileView.Initialize("ImportFileView")
    ImportFileView.Items.Clear                                        'Clear Listview before populating

    For FileN = 0 To FileListing1.Size-1                        'Builds the viewable list ImportFileView from the List "FileListing"
        Dim lbl As Label
        lbl.Initialize("")
        lbl.Style = lbl.Style & "-fx-background-color:Dark Grey"
        lbl.TextColor = fx.Colors.White
        temp = FileListing1.Get(FileN)                            'Get the value from the List
        lbl.Text = temp
        ImportFileView.Items.Add(lbl)
    Next
    ImportFileView.Visible = True

'    lblTitle.Text = "Select File"
'    lblTitle.Visible = True
'    lblTitle.Top = 0
''    ListBack.Visible = True
'    ListFlag = 1
End Sub
 

jmon

Well-Known Member
Licensed User
Longtime User
Looks ok to me.

The formatting of the CSS should be :
B4X:
lbl.Style = "-fx-background-color:Dark Grey;"
You forgot the semicolon at the end

[EDIT] where do you declare your listview?

[EDIT] If you already initialized your listview earlier, you shouldn't initialize it again here
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
jmon,

Thanks for the reply, I must have deleted ; when changing colour.
Have now thought that size and location are not defined? Is there a "BringToFront" option somewhere?
When I run the Sub nothing appears to happen.


Regards Roger
 
Upvote 0

jmon

Well-Known Member
Licensed User
Longtime User
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Thanks jmon,

That link you included has filled in a few blanks for me. It is now bookmarked. Unfortunately still no joy.
ImportFileView is declared in Process Globals, Initialized only where you see it and I have added the MainForm lines. The code now looks like this:

B4X:
Sub FileSelect
'Brings up ListView to allow selection of files.
'Called by BtnFiles_Action Sub
    Dim temp As String
    ImportFileView.Initialize("ImportFileView")
    MainForm.RootPane.AddNode(ImportFileView, 0, 0, 0, 0) 'values not important
    MainForm.RootPane.SetAnchors(ImportFileView, 0, 0, 320dip, 0)
    ImportFileView.Items.Clear                                        'Clear Listview before populating

    For FileN = 0 To FileListing1.Size-1                        'Builds the viewable list ImportFileView from the List "FileListing"
        Dim lbl As Label
        lbl.Initialize("")
        lbl.Style = lbl.Style & "-fx-background-color:Dark Grey;"
        lbl.TextColor = fx.Colors.White
        temp = FileListing1.Get(FileN)                            'Get the value from the List
        lbl.Text = temp
        ImportFileView.Items.Add(lbl)
    Next
    ImportFileView.Visible = True

'    lblTitle.Text = "Select File"
'    lblTitle.Visible = True
'    lblTitle.Top = 0
''    ListBack.Visible = True
'    ListFlag = 1
End Sub

Regards Roger
 
Upvote 0

jmon

Well-Known Member
Licensed User
Longtime User
I think that what you are trying to do is to show a nodal form isn't it?
You can check this tutorial https://www.b4x.com/android/forum/threads/modal-dialogs.34656/#content

Also if you are just looking at doing a file selection window you can use:
B4X:
Sub FileSelect
    Dim fc As FileChooser
    fc.Initialize
    fc.Title = $"Title"$
    Dim FileChosen As String = fc.ShowOpen(MainForm)
    If FileChosen <> "" Then
        'do your thing
    End If
End Sub

otherwise, if you just want to show a selection list you can use the new feature of b4j 4.00, the INPUT LIST:
https://www.b4x.com/android/forum/threads/msgbox-inputlist.61461/
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Hi jmon,

What I am trying to do is show a list of file names and have the user clicking the required file. I used ListView in B4A and thought ListView in B4J would be a similar beast.
You have provided a couple of very interesting alternatives. (Much reading after the Fat man and his raindeer have cleared the roof) However I am still perplexed as to why the ListView failed to appear, everything seems in order.

Many thanks Roger
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
ListView in B4J would be a similar beast.
It is similar but not the same

From a tutorial i found...

B4X:
' Needed sub
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(16)
  lbl1.Tag = Value
  ap.AddNode(lbl1, 0, 0, lv.Width, 20dip)
  lv.Items.Add(ap)
End Sub

Filling the listview
B4X:
    Dim l As List = File.ListFiles(datadir)
    For i = 0 To l.Size-1
        'Log(l.Get(i))
        Dim f As String = l.Get(i)
        listviewAddOneLine(lv1367,l.Get(i),l.Get(i))
    Next

And a listview click
B4X:
Sub lv1347_SelectedIndexChanged(Index As Int)
    driverinfo.Text = ""
    Dim ap As AnchorPane = lv1347.Items.Get(Index)   
   
    For Each n As Node In ap.GetAllViewsRecursive
        If n Is Label Then
            Dim lbl As Label = n
            Log(lbl.Tag)
        End If
    Next   
End Sub
 
Upvote 0

jmon

Well-Known Member
Licensed User
Longtime User
Try this (file attached):

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private btn1 As Button

    Private FormLV As Form 
 
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    MainForm.RootPane.LoadLayout("Main") 'Load the layout file.
    MainForm.Show
End Sub

Sub btn1_Action
    FileSelect
 
End Sub

Sub FileSelect
    'Create the modal form:
    FormLV.Initialize("", 300, 500)
    FormLV.SetFormStyle("UTILITY")

    'Create the listview:
    Private lv As ListView
    lv.Initialize("lv")

    'Add the listview:
    FormLV.RootPane.AddNode(lv, 0, 0, -1, -1)
    FormLV.RootPane.SetAnchors(lv, 0, 0, 0, 0)
 
    'Fill the data:
    For i = 0 To 100
        Dim lbl As Label
        lbl.Initialize("")
        lbl.Text = 12345 * Rnd(1, 999) 'just some random stuff
 
        lv.Items.Add(lbl)
    Next 
 
    'Show the modal form:
    FormLV.ShowAndWait
 
End Sub

Sub lv_SelectedIndexChanged(Index As Int)
    Dim lv As ListView = Sender
    Dim lbl As Label = lv.Items.Get(Index)
    Log($"Data clicked : ${lbl.Text}"$)
 
    'Close the modal form:
    FormLV.Close
End Sub
 

Attachments

  • LVTest.zip
    2 KB · Views: 198
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Try this (file attached):

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private btn1 As Button

    Private FormLV As Form

End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.SetFormStyle("UNIFIED")
    MainForm.RootPane.LoadLayout("Main") 'Load the layout file.
    MainForm.Show
End Sub

Sub btn1_Action
    FileSelect

End Sub

Sub FileSelect
    'Create the modal form:
    FormLV.Initialize("", 300, 500)
    FormLV.SetFormStyle("UTILITY")

    'Create the listview:
    Private lv As ListView
    lv.Initialize("lv")

    'Add the listview:
    FormLV.RootPane.AddNode(lv, 0, 0, -1, -1)
    FormLV.RootPane.SetAnchors(lv, 0, 0, 0, 0)

    'Fill the data:
    For i = 0 To 100
        Dim lbl As Label
        lbl.Initialize("")
        lbl.Text = 12345 * Rnd(1, 999) 'just some random stuff

        lv.Items.Add(lbl)
    Next

    'Show the modal form:
    FormLV.ShowAndWait

End Sub

Sub lv_SelectedIndexChanged(Index As Int)
    Dim lv As ListView = Sender
    Dim lbl As Label = lv.Items.Get(Index)
    Log($"Data clicked : ${lbl.Text}"$)

    'Close the modal form:
    FormLV.Close
End Sub

Will try it ASAP.

Regards Roger
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Thanks DonManfred,

I actually have an icon to B4J Howtos on line on my desktop but didn't think to use it. I think I will start afresh after the Christmas dust has settlled.
[Christmas day is only 3Hrs away here.]:)

Merry Christmas
Roger
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
HI All,

Below is the final out come. With the guidance provided by jmon and DonManfred I found I didn't need the ListView or even the Sub FileSelect. The result is much simpler than the B4A original, it all fits in the Sub that use to call FileSelect.
Originally I had an ImageView on a Pane on a Form. Loading the Image as the Form Background is simpler and seems to work OK. [So far]
The only problem now is when I try to lock the size of the Form with "FormLV.Resizable = False" the Form changes size before locking.
I will put this problem in a new post.


Regards Roger

B4X:
Sub BtnFiles_action
    If File.Exists(DirPath, "") = False Then File.MakeDir(DirPath, "")
'Find files. Select file to import
    Private Image1 As Image
    Private FileChooser1 As FileChooser
   
    FileChooser1.Initialize
    FileChooser1.Title = $"Saved Files"$
    FileChooser1.InitialDirectory = DirPath
    Filename1 = FileChooser1.ShowOpen(MainForm)
    Filename1 = Filename1.Replace(DirPath, "")
    If Filename1 <> "" Then
        Image1.initialize(DirPath, Filename1)
        FormLV.Initialize("", Image1.Width, Image1.Height)
        FormLV.SetFormStyle("")
        FormLV.Title = Filename1
        FormLV.RootPane.Style = "-fx-background-image: url('" & File.GetUri(DirPath, Filename1) & "');"
        FormLV.Show
        FormLV.Resizable = False
    End If
End Sub
 
Upvote 0
Top