Android Question [SOLVED] Help with For and Variables

German Buchmuller

Member
Licensed User
Hi, I my idea is to create 5 imageviews with using for i=0 to 4, while getting all of these imageviews with a different name. Example:
B4X:
For i=0 to 4
Dim im(i) As ImageView
            im(i).Initialize("im")
            im(i).Bitmap=LoadBitmap(File.DirAssets,"argentina.png")
            scrollviewff.Panel.AddView(im(i),2%x,15%y*i,96%x,20%y)
The problem is that I get an error. I believe that making it work is not as simple as i thought to. Any idea?
 

OliverA

Expert
Licensed User
What error? You have not posted any error messages
 
Upvote 0

OliverA

Expert
Licensed User
That's logical. If you say dim myArray(1) as ImageView, then the first array element is myArray(0). I think what you are trying to accomplish is the following
B4X:
Dim im(5) As ImageView
For i = 0 to 4
  Dim iv as ImageView
  iv.Initialize("im")
  iv.Bitmap=LoadBitmap(File.DirAssets,"argentina.png")
  scrollviewff.Panel.AddView(iv,2%x,15%y*i,96%x,20%y)
  im(i) = iv
Next
Note: Not tested. Also, what would you need im for? As you can see, you can create 5 ImageViews, load a bitmap to them and add then to a panel without needing the array.
 
Upvote 0

German Buchmuller

Member
Licensed User
That's logical. If you say dim myArray(1) as ImageView, then the first array element is myArray(0). I think what you are trying to accomplish is the following
B4X:
Dim im(5) As ImageView
For i = 0 to 4
  Dim iv as ImageView
  iv.Initialize("im")
  iv.Bitmap=LoadBitmap(File.DirAssets,"argentina.png")
  scrollviewff.Panel.AddView(iv,2%x,15%y*i,96%x,20%y)
  im(i) = iv
Next
Note: Not tested. Also, what would you need im for? As you can see, you can create 5 ImageViews, load a bitmap to them and add then to a panel without needing the array.
Thanks for the reply! What I want to do is to download 5 images from my http server, and show them on 5 imagviews on a scrollview. Then, I want a to be able to click the imageview and know which one of the 5 imageviews was the one clicked.

B4X:
    Dim im(6) As ImageView
            For i = 0 To File.ReadString(File.DirInternal,"ffnumerodecombos.txt")
                Dim iv As ImageView
                iv.Initialize("im")
                iv.Bitmap=LoadBitmap(File.DirAssets,"argentina.png")
                scrollviewff.Panel.AddView(iv,2%x,15%y*i,96%x,20%y)
                im(i) = iv
            Dim links As Map
            links.Initialize
            links.Put(im(i), "http://g3dsoftware.ddns.net:8080/neater/hola"&i&".png")
            CallSubDelayed2(ImageDownloader, "Download", links)
            Next

Sub im_Click
(here I want to know wether it was im(1) or im(2) or im(3) and so on, the one clicked.
End Sub
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
What I want to do is to download 5 images from my http server, and show them on 5 imagviews on a scrollview.

So why do you declare 6 ImageViews.. ?? Dim im(6) As ImageView

Will it ALWAYS be 5 imageviews ??

Will the value of File.ReadString(File.DirInternal,"ffnumerodecombos.txt") ... Always be 5 ? if so why not just use ..
B4X:
For i = 0 to 4     '  = 5

Also a Tip (this might not apply to your final code) ... when you show the same image in different imageViews .. load it Once only then reuse the Bitmap ie:
B4X:
Dim bmp As Bitmap = LoadBitmap(File.DirAssets,"argentina.png")
  '.........
im(i).Bitmap= bmp


But in answer to your last post .. Use the .Tag property to store information which can be extracted later ...

B4X:
'if you are loading a different image each time .. this will happen in the For loop as per your original code.
Dim bmp As Bitmap = LoadBitmap(File.DirAssets,"argentina.png")
Dim im(5) As ImageView
For i = 0 To 4  ' OR File.ReadString(File.DirInternal,"ffnumerodecombos.txt") -1
    im(i).Initialize("im")
    im(i).Bitmap= bmp
    im(i).Tag = "http://g3dsoftware.ddns.net:8080/neater/hola"&i&".png"
    scrollviewff.Panel.AddView(im(i),2%x,15%y*i,96%x,20%y)
 
    Dim links As Map  'is the map neccessary now ?? .. or just pass im(i).Tag value ?
    links.Initialize
    links.Put(im(i), "http://g3dsoftware.ddns.net:8080/neater/hola"&i&".png")
    CallSubDelayed2(ImageDownloader, "Download", links)
Next
End Sub
 
Sub im_Click
    Dim iv As ImageView = Sender
    Log (iv.tag)
End Sub


PS .. afterthought ... If you find your want to load a random set of images , maybe something like this worked into your code.
B4X:
Dim numImages As Int =  File.ReadString(File.DirInternal,"ffnumerodecombos.txt")
Dim bmp As Bitmap = LoadBitmap(File.DirAssets,"argentina.png")
Dim im(numImages) As ImageView
For i = 0 To numImages - 1
'...................................



It might be wise to start a new thread for any New Questions ...
 
Last edited:
Upvote 0

German Buchmuller

Member
Licensed User
So why do you declare 6 ImageViews.. ?? Dim im(6) As ImageView

Will it ALWAYS be 5 imageviews ??

Will the value of File.ReadString(File.DirInternal,"ffnumerodecombos.txt") ... Always be 5 ? if so why not just use ..
B4X:
For i = 0 to 4     '  = 5

Also a Tip (this might not apply to your final code) ... when you show the same image in different imageViews .. load it Once only then reuse the Bitmap ie:
B4X:
Dim bmp As Bitmap = LoadBitmap(File.DirAssets,"argentina.png")
  '.........
im(i).Bitmap= bmp


But in answer to your last post .. Use the .Tag property to store information which can be extracted later ...

B4X:
'if you are loading a different image each time .. this will happen in the For loop as per your original code.
Dim bmp As Bitmap = LoadBitmap(File.DirAssets,"argentina.png")
Dim im(5) As ImageView
For i = 0 To 4  ' OR File.ReadString(File.DirInternal,"ffnumerodecombos.txt") -1
    im(i).Initialize("im")
    im(i).Bitmap= bmp
    im(i).Tag = "http://g3dsoftware.ddns.net:8080/neater/hola"&i&".png"
    scrollviewff.Panel.AddView(im(i),2%x,15%y*i,96%x,20%y)
 
    Dim links As Map  'is the map neccessary now ?? .. or just pass im(i).Tag value ?
    links.Initialize
    links.Put(im(i), "http://g3dsoftware.ddns.net:8080/neater/hola"&i&".png")
    CallSubDelayed2(ImageDownloader, "Download", links)
Next
End Sub
 
Sub im_Click
    Dim iv As ImageView = Sender
    Log (iv.tag)
End Sub


PS .. afterthought ... If you find your want to load a random set of images , maybe something like this worked into your code.
B4X:
Dim numImages As Int =  File.ReadString(File.DirInternal,"ffnumerodecombos.txt")
Dim bmp As Bitmap = LoadBitmap(File.DirAssets,"argentina.png")
Dim im(numImages) As ImageView
For i = 0 To numImages - 1
'...................................



It might be wise to start a new thread for any New Questions ...
Worked perfectly!!! Thank you very much!
 
Upvote 0
Top