Android Question Nested loop

Sub7

Active Member
Licensed User
Sorry for the dumb question, i know is something elementary but i'm stuck.

B4X:
Dim maxn As Int = 5
For i = 0 To  5
i = i+1

    For k = 0 To maxn
    Log(k)
    Next

Next
The output is
-0 1 2 3 4 5
- 0 1 2 3 4 5
- 0 1 2 3 4 5

While i want the inner loop to generate:

- 0 1 2 3 4
- 5 6 7 8 9
- 10 11 12 13 14 and so on.

Thanks :p
 

sorex

Expert
Licensed User
B4X:
Dim maxn As Int = 5
For i = 0 To  5
'i = i+1   remove this

    For k = 0 To maxn
    Log(i*5+k)
    Next

Next
 
Last edited:

Sub7

Active Member
Licensed User
B4X:
Dim maxn As Int = 5
For i = 0 To  5
i = i+1

    For k = 0 To maxn
    Log(i*5+k)
    Next

Next
Hi Sorex :) look this

5
6
7
8
9
10

15
16
17..


While i want the inner loop to generate:

- 0 1 2 3 4
- 5 6 7 8 9
- 10 11 12 13 14

Thx :p
 

sorex

Expert
Licensed User
that's because you have an unneeded i=i+1 there

you also might need for k=0 to ...-1 depending on how you definded that variable (taking into account that it start from 0 or 1)
 

Sub7

Active Member
Licensed User
B4X:
Dim maxn As Int = 5
For i = 0 To  5
  For k = 0 To maxn -1
  Log(i*5+k)
  Next
   Log("five blocks")
Next
 

Sub7

Active Member
Licensed User
hmmm it's more tricky that what i tought, this is what i wanted to do:
I have to load 50 different images, to make this works i think i need five inner loops-

B4X:
Dim maxn As Int = 5
For i = 0 To  5

     Dim p As Panel
     p.Initialize("")
     p.color = Colors.ARGB(255,235,235,235)
     clv.Add(p, 71dip, "test")
     p.LoadLayout("clv_layout")


  For k = 0 To maxn -1
       Dim g As Int = i*5+k
       img_ornnament1.Bitmap = LoadBitmapSample(File.DirAssets, "img_s/"& g&"_image.png", 30, 30)   
       img_ornnament2.Bitmap = LoadBitmapSample(File.DirAssets, "img_s/"& g&"_image.png", 30, 30)
       img_ornnament3.Bitmap = LoadBitmapSample(File.DirAssets, "img_s/"& g&"_image.png", 30, 30)
       img_ornnament4.Bitmap = LoadBitmapSample(File.DirAssets, "img_s/"& g&"_image.png", 30, 30)
       img_ornnament5.Bitmap = LoadBitmapSample(File.DirAssets, "img_s/"& g&"_image.png", 30, 30)
  Next

   Log("five blocks")
   
Next
 

Sub7

Active Member
Licensed User
I feel stupid is just best work with an array of views. like tic tac toe example.
 

Sub7

Active Member
Licensed User
Sorry, i am trying to load 50 different images in a customlistview, five images each row.
I have named the images in this way 0_image 1_image to concatenate the filename inside a loop.
 

udg

Expert
Licensed User
Do you want to arrange 50 elements on 10 rows of 5 items each?
Something like?
B4X:
for i= 0 to maxitems-1 'maxitems = 50
  row = floor(i/5)  ' 0,1,2,3,4,5,6,7,8,9
  col = i mod 5     ' 0,1,2,3,4
  log(row&" "&col)
next
 

Sub7

Active Member
Licensed User
Yes thanks both. i am trying to arrange 50 elements on 10 rows of 5 items each.
Tryng... :)
 

Sub7

Active Member
Licensed User
I solved in this way

B4X:
For x = 0 To p.NumberOfViews -1
   p.GetView(x).SetBackgroundImage(LoadBitmapSample(File.DirAssets, "img_s/"& x&"_image.png", 30, 30))
Next
 
Top