Problem: adding ListView with the designer

specci48

Well-Known Member
Licensed User
Longtime User
I took the ListView example from the tutorials section and removed the "AddView(ListView1,...)".
I added a ListView1 with the designer instead but if I try to run that change on the emulator no lines are shown.
Why?


specci48

Edit: Added the missing layout... :signOops:
 

Attachments

  • ListView-Designer.zip
    17.4 KB · Views: 278
Last edited:

Kevin

Well-Known Member
Licensed User
Longtime User
I just thought I'd add that I just had this problem yesterday. Couldn't figure out why there were no lines, and also the listview wasn't filling when adding items. I never did figure it out exactly, so I wound up deleting the one I created in the Designer and just "coded" a new one. After reading this thread though, I think the problem may have been me "initializing" it when I didn't need to. (?)

I'm coming from VB and while I am struggling a bit with the transition, I must say that while I am getting stuck doing "simple" things that would be a no-brainer in VB, it isn't taking me too long to 'figure it out' in B4A. Definitely a bit of a learning curve, but thankfully not nearly as steep as 'normal' Java/Android programming.

And not to highjack this thread, but I'm having a problem finding a simple, straightforward answer to how I go about 'storing' the listview contents and then 'restoring' them after the app is "paused" (such as when device is rotated). I could swear I saw something about this the other day but I can't find it now.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
And not to highjack this thread, but I'm having a problem finding a simple, straightforward answer to how I go about 'storing' the listview contents and then 'restoring' them after the app is "paused" (such as when device is rotated). I could swear I saw something about this the other day but I can't find it now.
When you add the items to the ListView, also add them to a process global List.
The List will be kept when the device rotates. So in Activity_Create you can read the values from the List and add them to the ListView.
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
For each item in the listview, I am showing a show "title" with a show "description" on the 2nd line (smaller text) and it adds a link to the list item's value.

ListView1.AddTwoLines2(Title, Desc, Link)

So would I need to create 3 lists in the process_globals sub?

Such as:

List1.Add (Title)
List2.Add (Desc)
List3.Add (Link)

or does the "list" store all variables related to each listview item? If that's the case, I guess I'm not understanding how to add all three things related to the ListView item to the one single item in the "list". Sample code please?

Thanks!
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can create your own Type and then store variables of that type:
B4X:
Sub Process_Globals
    Type Show (Title As String, Description As String, Link As String)
    Dim lstShows As List
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim ListView1 As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        lstShows.Initialize
        'load the data from somewhere
        For i = 1 To 40
            Dim s As Show
            s.Initialize
            s.Title = "title"
            s.Description = "sdf sdf sdf "
            s.Link = "http://///"
            lstShows.Add(s)
        Next
    End If
    'Activity.LoadLayout(...)
    'now fill the ListView from this list (which will be kept when the activity is recreated)
    For i = 0 To lstShows.Size - 1
        Dim s As Show
        s = lstShows.Get(i)
        ListView1.AddTwoLines2(s.Title, s.Description, s)
    Next
End Sub
 
Upvote 0

CidTek

Active Member
Licensed User
Longtime User
You can create your own Type and then store variables of that type:
B4X:
        lstShows.Initialize
        'load the data from somewhere
        For i = 1 To 40
            Dim s As Show
            s.Initialize
            s.Title = "title"
            s.Description = "sdf sdf sdf "
            s.Link = "http://///"
            lstShows.Add(s)
        Next
    'now fill the ListView from this list (which will be kept when the activity is recreated)
    For i = 0 To lstShows.Size - 1
        Dim s As Show
        s = lstShows.Get(i)
        ListView1.AddTwoLines2(s.Title, s.Description, s)
    Next

Erel, is there a reason you repeatedly declare and initilize "s" within the loop?
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
To take a quote from this thread:

All types can be treated as Objects.
Collections like lists and maps work with Objects and therefore can store any value.
Here is an example of a common mistake, where the developer tries to add several arrays to a list:

B4X:
Dim arr(3) As Int
Dim List1 As List
List1.Initialize
For i = 1 To 5
 arr(0) = i * 2
 arr(1) = i * 2
 arr(2) = i * 2
 List1.Add(arr) 'Add the whole array as a single item
Next
arr = List1.Get(0) 'get the first item from the listLog(arr(0)) 'What will be printed here???
You may expect it to print 2. However it will print 10.
We have created a single array and added 5 references of this array to the list.
The values in the single array are the values set in the last iteration.
To fix this we need to create a new array each iteration.
This is done by calling Dim each iteration:

B4X:
Dim arr(3) As Int 'This call is redundant in this case.
Dim List1 As List
List1.Initialize
For i = 1 To 5
 Dim arr(3) As Int
 arr(0) = i * 2
 arr(1) = i * 2
 arr(2) = i * 2
 List1.Add(arr) 'Add the whole array as a single item
Next
arr = List1.Get(0) 'get the first item from the list
Log(arr(0)) 'Will print 2
Which explains everything.
 
Upvote 0
Top