Problem populating ScrollView from array

William Hunter

Active Member
Licensed User
Longtime User
:sign0104: I am new to B4A and need a little assistance. I am trying to populate a ScrollView with CheckBoxes, with strings contained in a List(Array). The List(Array) is properly formed, and I can Get the strings to display in the ScrollView. Let’s say that I have ten items in the array. The problem I am having, is that instead of displaying the ten array items sequentially in list form, all ten listed ScrollView items display the content of Array(0). Then all ten items in the list are overwritten with the content of Array(1), and so on, with the remaining items in the array, until the full ten travels of the loop. What is missing in my loop that causes this? Any help would be greatly appreciated. My code is below:
B4X:
Sub Populate_ScrollView()
   Dim ScrollView1 As ScrollView
    Dim lstChecks As List
    Dim height As Int : height = 50dip
    Dim pnl As Panel
   ScrollView1.Initialize(0)
    pnl = ScrollView1.Panel
    Activity.AddView(ScrollView1, 0, 0, 100%x, 100%y)
    lstChecks.Initialize
   nbIndex = 0
    For i = 1 To MsgNb
       Dim chk As CheckBox
       chk.Initialize("chkchng")
      hSubjectItem = hSubjectList.Get(nbIndex) 'get the item
      chk.Text = hSubjectItem
      chk.tag = i
      lstChecks.Add(chk)
       Dim lbl1 As Label
      lbl1.Initialize("")
      hFromItem = hFromList.Get(nbIndex) 'get the item
      lbl1.Text = hFromItem
      lbl1.Gravity = Gravity.CENTER_VERTICAL
      pnl.AddView(chk, 0, height * (i - 1), 120dip, height)
      pnl.AddView(lbl1, 125dip, height * (i - 1), 120dip, height)
      nbIndex = nbIndex + 1
   Next
    pnl.height = lstChecks.Size * height
End Sub
 
Last edited by a moderator:

William Hunter

Active Member
Licensed User
Longtime User

Attachments

  • Testing.zip
    6.6 KB · Views: 229
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code seems strange:
B4X:
Sub pop_Download2Completed(Success As Boolean, MessageId As Int, Message As String, MainHeaders As Map)
   hSubjectList.Initialize
   hFromList.Initialize
   nbX = 1
   For i = 1 To MsgNb
      If nbX > 1 Then
         pop.Initialize("pop.server.address", 110, "username", "password", "pop")
         pop.DownloadMessage2(nbX, 0, True, False)
      End If
      hSubjectItem = (MainHeaders.Get("SUBJECT"))
      hSubjectList.Add(hSubjectItem) 'Add item to array
      hFromItem = (MainHeaders.Get("FROM"))
      hFromList.Add(hFromItem) 'Add item to array
      pop.Close 'The connection will be closed
      UserDefined.Wait(1) ' 1 Second sleep to allow release
      nbX = nbX +1
   Next
   
   If Success = False Then
        ToastMessageShow(LastException.Message, True)
    Else
      CallSub("Main", "Populate_ScrollView")
   End If
End Sub

1. This is not an error, but why do you use CallSub? You can just write Populate_ScrollView

2. The For loop looks wrong to me. I'm not sure what is the syntax of DownloadMessage2 but you should remember that the messages are not immediately downloaded. You should probably download a single message each time.
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
This code seems strange:
1. This is not an error, but why do you use CallSub? You can just write Populate_ScrollView

2. The For loop looks wrong to me. I'm not sure what is the syntax of DownloadMessage2 but you should remember that the messages are not immediately downloaded. You should probably download a single message each time.

Hello Erel – Thank you for your response.

1. I had read the following in Code – Keywords: “CallSub can also be used to call subs in the current module.” I took this to be the rule. Your explanation of the way a Sub in the Main module is called, is the same as the way to call a Function in another Basic environment that I work in. I should have tried this first. I must be over-thinking. My bad.

2. I’m not using B4A’s native Net Library. I’m using Wim Lambrecht’s wPOP Library. I know my code looks awkward, but in order to match the signature required by wPOP, I had to do things this way. I am only looking to retrieve header info from the server, and configure it into two arrays (could be a two element array). I then want to list this header info in a ScrollView with CheckBoxes. This is when I start to have problems. The ScrollView will not display the header info correctly. Each array item is displayed in every item of the list, and then the next array item overwrites each item listed, and so on until the loop cycle is totally completed. The array items do not list sequentially in the ScrollView list.

I’m trying to create a small application to delete junk mail from the server, prior to retrieving messages with a mail client. What I thought would be not all that difficult is giving me a hard time. I hope you will have some words of wisdom for me, and look forward to your reply.
 
Upvote 0
Top