Android Question Adding dynamic view to loaded layout ?

MitchBu

Well-Known Member
Licensed User
Longtime User
I am trying to apply the tutorial here https://www.b4x.com/android/forum/threads/listview-tutorial.6537/ but I would like to instanciate the ListView in an existing layout.

Obviously the listview exists, as I can see it's row when I tap where I know it is.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Activity.LoadLayout("Main")
    ListView1.Initialize("ListView1")
    For i = 1 To 300
        ListView1.AddSingleLine("Item #" & i)
    Next
    Activity.AddView(ListView1, 0, 0, 100dip, 500dip)
    ListView1.Visible = True
    ListView1.BringToFront
End Sub

Apparently, I should attach the view to Main, and not the activity, but I don't know how to proceed. Is it possible, and if so, how do I do that ?

TIA
 

klaus

Expert
Licensed User
Longtime User
Me too, I don't understand exactly the purpose of what you want to do.
The code snippet in your first post brings no difference with the ListView added in the Layout in the Designer.
If you add it directly in the Layout and set Visibility to False or add it in Activity_Create you should also set Visibility to False.
The Advantage of adding it in the Designer would be the AutoScaling which is not done when you add the ListView in the code.
Apparently, I should attach the view to Main
Main is an Activity module, so you need to add the ListView to Activity!
 
Upvote 0

Widget

Well-Known Member
Licensed User
Longtime User
I am trying to apply the tutorial here https://www.b4x.com/android/forum/threads/listview-tutorial.6537/ but I would like to instanciate the ListView in an existing layout.

Obviously the listview exists, as I can see it's row when I tap where I know it is.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Activity.LoadLayout("Main")
    ListView1.Initialize("ListView1")
    For i = 1 To 300
        ListView1.AddSingleLine("Item #" & i)
    Next
    Activity.AddView(ListView1, 0, 0, 100dip, 500dip)
    ListView1.Visible = True
    ListView1.BringToFront
End Sub

Apparently, I should attach the view to Main, and not the activity, but I don't know how to proceed. Is it possible, and if so, how do I do that ?

TIA

Your code should work.

Make sure you DON'T HAVE a ListView1 defined in Designer and make sure you have "Private ListView1 as ListView" defined in Globals. You are probably missing something simple. If you care to zip up your sample app using File > Export As Zip > Include Shared Modules then we can take a look at it.
 
Upvote 0
Top