List and For Loop

JonPM

Well-Known Member
Licensed User
Longtime User
I've been going over this for some time but just can't seem to figure it out.

B4X:
Sub UpdateMainList
   If File.Exists(File.DirInternal,"HideList.txt") = True Then
      HideList = File.ReadList(File.DirInternal,"HideList.txt")
      Dim Hidden As String
      Dim i As Int
      Dim n As Int
      For i = 0 To HideList.Size-1
         Hidden = HideList.Get(i)
         For n = 0 To MainList.Size-1
            If Hidden = MainList.Get(n) Then
            MainList.RemoveAt(n)
            End If
         Next
      Next
   End If
End Sub

I keep getting an IndexOutOfBoundsException for the MainList (index is 40, size is 40).

Here is some background on the app:
I have a scrollview with a bunch of buttons, and I want to let the user hide buttons if they choose. Now, in the above code, if I change the line to MainList.Size-2, then the code works almost perfectly. The problem is that you can't hide the very last button. If you try nothing happens and the button remains on the screen.

Here is the code I am using to hide the buttons:
B4X:
Sub Button_LongClick
    Dim Send As Button
   Dim MsgHide As Int
    Send = Sender
    MsgHide = Msgbox2("Hide Button?","","Yes","","No",Null)
      If MsgHide = DialogResponse.NEGATIVE Then
         Return True
      Else
         HideList.Add(Send.Text)
         File.WriteList(File.DirInternal,"HideList.txt",HideList)
         ClearButtons
         UpdateMainList
         CreateButtons
      End If   
End Sub

I'm sure it's something very obvious, but I've been staring at it for far too long. Any help would be great.
 

derez

Expert
Licensed User
Longtime User
I think the problem is that the loop goes to the full size of the list but when you remove items inside the loop the list size is decreased but not the limit and the index is not valid any more.

Try to do the loop backwards, from size -1 to 0 step -1, then the index is always valid.
 
Upvote 0

JonPM

Well-Known Member
Licensed User
Longtime User
I think the problem is that the loop goes to the full size of the list but when you remove items inside the loop the list size is decreased but not the limit and the index is not valid any more.

Try to do the loop backwards, from size -1 to 0 step -1, then the index is always valid.


Looks like that did the trick! thanks!

Sent from my DROIDX
 
Upvote 0

JonPM

Well-Known Member
Licensed User
Longtime User
Having another problem now.

I am using this code below to allow the users to unhide the hidden buttons:
B4X:
Sub mnuUnhide_Click
   If File.Exists(File.DirInternal,"HideList.txt") = True Then
      HideList = File.ReadList(File.DirInternal,"HideList.txt")
      Dim result As List
      result = InputMultiList(HideList,"Hidden Buttons")
      For i = result.Size-1 To 0 Step-1
         MainList.Add(HideList.Get(i))
         MainList.SortCaseInsensitive(True)
         HideList.RemoveAt(result.Get(i))
         File.WriteList(File.DirInternal,"HideList.txt",HideList)
      Next
      ClearButtons
      UpdateMainList
      CreateButtons
   Else
      Msgbox("No hidden buttons found","")
   End If

End Sub

This code does the job great except under one condition. If the button's text contains a space (i.e. "Button One"), it does not get added to the MainList (however it does still get removed from the HideList). In every other instance (i.e. "ButtonTwo","ButtonThree",etc), the code works just as planned (that is the button gets added to MainList, and removed from HideList). Is there a way to fix this problem? I'm not sure why the space is causing this error.
 
Upvote 0

JonPM

Well-Known Member
Licensed User
Longtime User
Why are you calling File.WriteList every iteration?

Put a breakpoint on the MainList.Add line and see the values that get in.

Ok so I tried this code:
B4X:
Sub mnuUnhide_Click
   If File.Exists(File.DirInternal,"HideList.txt") = True Then
      HideList = File.ReadList(File.DirInternal,"HideList.txt")
      Dim result As List
      result = InputMultiList(HideList,"Hidden Buttons")
      For i = result.Size-1 To 0 Step-1
         MainList.Add(HideList.Get(i))
         Msgbox(MainList,"MainList")
         MainList.SortCaseInsensitive(True)
         HideList.RemoveAt(result.Get(i))
         Msgbox(HideList,"HideList")
      Next
      File.WriteList(File.DirInternal,"HideList.txt",HideList)
      ClearButtons
      UpdateMainList
      CreateButtons
   Else
      Msgbox("No hidden buttons found","")
   End If

End Sub

And just like I stated before, when a button's text has a space in it (in this example I used "Test Button"), it doesn't get added to MainList (which I confirmed looking at the msgbox. However it does get removed from HideList (again verified by msgbox). I assume it has no problem getting removed from HideList because the coding removes it at the index, and not by text.
 
Upvote 0

JonPM

Well-Known Member
Licensed User
Longtime User
Actually I just found the problem. Here is the working code:

B4X:
Sub mnuUnhide_Click
   If File.Exists(File.DirInternal,"HideList.txt") = True Then
      HideList = File.ReadList(File.DirInternal,"HideList.txt")
      Dim result As List
      result = InputMultiList(HideList,"Hidden Buttons")
      For i = result.Size-1 To 0 Step-1
         MainList.Add(HideList.Get(result.Get(i)))
         MainList.SortCaseInsensitive(True)
         HideList.RemoveAt(result.Get(i))
      Next
      File.WriteList(File.DirInternal,"HideList.txt",HideList)
      ClearButtons
      UpdateMainList
      CreateButtons
   Else
      Msgbox("No hidden buttons found","")
   End If

End Sub
 
Upvote 0
Top