Android Question ImageView(variable)

Mr Blue Sky

Active Member
Licensed User
Longtime User
Hi, I would like support to write correctly ImageView1 = ImageView(i) and LabelView(i)
Thanks to all


Dim i As Int = 4

Do While i < 37
If File.Exists(File.DirRootExternal,Cur.GetString("REF") & Cur.GetColumnName(i) & ".jpg") Then
ImageView1.Bitmap = LoadBitmap(File.DirRootExternal, Cur.GetString("REF") & Cur.GetColumnName(i) & ".jpg")
End If
LabelView1.Text = Cur.GetString("REF") & Cur.GetColumnName(i) & ".jpg"
i=i+1
Loop
 
Last edited:

Mr Blue Sky

Active Member
Licensed User
Longtime User
Dim ImageView1 AS ImageView
Dim ImageView2 AS ImageView
Dim ImageView3 AS ImageView
Dim ImageView4 AS ImageView
...........................................

Dim ImageViews() As ImageView
ImageViews = Array As ImageView(ImageView1, ImageView2, ImageView3, ImageView4,..............................)

ImageViews(i).Bitmap = LoadBitmap(File.DirRootExternal, Cur.GetString("REF") & Cur.GetColumnName(i) & ".jpg")

This does not work well, and I have more than 100 photos for each line, this makes a lot of global variable ImageView1 to ImageView100.
There must surely be a simpler trick !
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
If I understood well, do something similar to this. There's no need to declare the views one by one

B4X:
Dim ImageViews(100) as ImageView
Dim i as int
for i=0 to 100
  ImageViews(i).initialize("")   'Assuming that you don't need to use touch events
  ImageViews(i).Bitmap = LoadBitmap(......)
  ....
next
 
Upvote 0

Mr Blue Sky

Active Member
Licensed User
Longtime User
Thank JordiCP This work is very cool and works very well, only now as you supose I use touch events to save or replace a photo in the bd or on the disk

Sub ImageView1_Click
RemoveViews
Activity.LoadLayout("camera")
InitializeCamera
End Sub
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Then you need a way to identify which imageview raised the event ;). You can do this

B4X:
Dim ImageViews(100) as ImageView
Dim i as int
for i= 0 to 100
ImageViews(i).initialize("ImageView1")  '<--- EDIT: forgot to add the event prefix
ImageViews(i).Bitmap = LoadBitmap(......)
ImageViews(i).Tag=i
...
Next


Sub ImageView1_Click
  Dim IV as ImageView = Sender
  Dim myIndex as int = IV.Tag
  Log("The clicked imageview index is "&myIndex)     'You can know which was the pressed imageview index and make this var global if needed elsewhere
  RemoveViews
  Activity.LoadLayout("camera")
  InitializeCamera
End Sub
 
Last edited:
Upvote 0

Mr Blue Sky

Active Member
Licensed User
Longtime User
Hi JordiCP sorry this code worked only one day

Dim ImageView(4) As ImageView
Dim i As Int
For i= 1 To 4
If File.Exists(File.DirRootExternal,Cur.GetString("REF") & Cur.GetColumnName(i+3) & ".jpg") Then
ImageView(i).Bitmap = LoadBitmap(File.DirRootExternal, Cur.GetString("REF") & Cur.GetColumnName(i+3) & ".jpg")
End If
Next

Yesterday this message : java.lang.ArrayIndexOutOfBoundsException: length=1; index=1

And today only this : java.lang.RuntimeException: Object should first be initialized (ImageView)

I have nothing to change ? ImageView Layout not available
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
As you have a large number (and perhaps variable) number of ImageViews I assume that you want to create them programatically, not with the designer.
So, they must be previously initialized and added to the activity.

B4X:
Dim ImageView(4) As ImageView
Dim i As Int
For i= 1 To 4
ImageView(i).initialize
Activity.AddView( Imageview(i), 0, (i-1)*25%Y, 50%X, 25%Y ) '<-- just as an example, change according to their desired shape, size and parent
If File.Exists(File.DirRootExternal,Cur.GetString("REF") & Cur.GetColumnName(i+3) & ".jpg") Then
ImageView(i).Bitmap = LoadBitmap(File.DirRootExternal, Cur.GetString("REF") & Cur.GetColumnName(i+3) & ".jpg")
End If
Next
 
Upvote 0
Top