Android Question Need help with for loop

apty

Active Member
Licensed User
Longtime User
I have a for loop that loops through items. The items could be any size. i can loop through all items as shown in the code below
B4X:
 For i = 0 To items.Size - 1
displayinpanel

I however want to pick the first 20 items, write them somewhere (e.g. in a panel with listview), then the next twenty and write to a different panel with a listview etc until i have picked all the items without repeating.

Kindly assist with a way to do this.
 

Ed Brown

Active Member
Licensed User
Longtime User
Hello @apty

The following adds on to @Roycefer's code example. It's a bit longer but it does not assume that you have items in multiples of 20.

B4X:
    Dim k As Int
    Dim n As Int = items.Size - 1
    For i = 0 To n Step 20       ' step each 20
        k = i + 19
       
        ' check if we're nearing the end to prevent out of bounds errors
        If k >= n Then k = n - i
       
        For j = 0 To k
            Stuff(items.Get(i + j)
        Next
    Next
 
Upvote 0

apty

Active Member
Licensed User
Longtime User
Thanks Ed,
I still don't understand this part:
Stuff(items.Get(i + j)

Instead of that, can i have a variable that i assign the next range e.g. after first 20, it will keep 21-40 then 40-60 etc?
 
Upvote 0

Ed Brown

Active Member
Licensed User
Longtime User
The line is simply a piece of junk code to demonstrate the use of the indices 'i' and 'j' (which you should probably rename to something more meaningful in your code). You can put whatever you like in there.
 
Upvote 0

apty

Active Member
Licensed User
Longtime User
Thanks
If i use items.Get(i) i get only 3 items, same case if i use items.Get(k). items.Get(j) doesnt work as the application force closes. Also items.Get(i + j) doesnt work.
I want to load the 20 items in a listview
 
Upvote 0

Ed Brown

Active Member
Licensed User
Longtime User
Could you please post your code? This way we can all see what you are trying to achieve and offer you better support.
 
Upvote 0

apty

Active Member
Licensed User
Longtime User
Ok. Here is a sample code that i was testing:
B4X:
lv.Clear
    Dim pm As PackageManager
    Dim packages As List
    packages = pm.GetInstalledPackages
   
   Dim k As Int
  Dim n As Int = packages.Size- 1
  For i = 0 To n Step 20  ' step each 20
  k = i + 19
   
  ' check if we're nearing the end to prevent out of bounds errors
   
  If k >= n Then k = n - i
  For j = 0 To k
    Dim p As String = packages.Get(i)
        Dim bdw As BitmapDrawable = pm.GetApplicationIcon(p)
        lv.AddTwoLinesAndBitmap(pm.GetApplicationLabel(p),packages.Get(i),bdw.Bitmap)

    Next
Next
 
Upvote 0

Ed Brown

Active Member
Licensed User
Longtime User
Change these lines
B4X:
Dim p as String = packages.Get(i)
...
lv.AddTwoLinesAndBitmap(pm.GetApplicationLabel(p),packages.Get(i),bdw.Bitmap)

to
B4X:
Dim p as String = packages.Get(i+j)
...
lv.AddTwoLinesAndBitmap(pm.GetApplicationLabel(p),packages.Get(i+j),bdw.Bitmap)

'i' will be in increments of 20 starting from 0. ie. 0, 19, 39, 59...
'j' is a counter from 0 to 19 (total 20)
so your index calculation needs to be i+j otherwise you'll only be adding every 20th element in your List starting with the first.
 
Upvote 0

apty

Active Member
Licensed User
Longtime User
If i add (i+j) as above, i get the error java.lang.idexoutofboundsexception. It says "invalid index 220,size 220"
 
Upvote 0

Ed Brown

Active Member
Licensed User
Longtime User
sorry... I tested the code using a list containing a multiple of 20

change this line
B4X:
If k >= n then k = n - i

to

B4X:
If k+i >= n then k = n - i
My apologies for that.
 
Upvote 0

apty

Active Member
Licensed User
Longtime User
Thanks a lot. Though it works, the listview is filled with all the items. I wanted only 20 items in thhe listview, then the next 20 i will put in another listview.
 
Upvote 0

Ed Brown

Active Member
Licensed User
Longtime User
Hello @apty

The code sample provided allows you to grab items in lots of 20. To add them to different listviews simply create a new listview for each lot and add each item while iterating through a lot.
 
Upvote 0
Top