Looped images click event

thughesimsuk

Member
Licensed User
Longtime User
Firstly, I will explain what I am trying to achieve, I have a sql loop which draws out rows of data, for each row I have a blank png image, when the user clicks on the image I want a tick mark to appear(image swap), I dont know how to make each image call a sub that will only update the image a user has clicked on, my code is as follows,
B4X:
For i = 0 To Cursor1.RowCount -1 


 img_blank_yes.Initialize("YesImg")
 img_blank_yes.Bitmap = LoadBitmap(File.DirAssets, "yes_blank.png")
 img_blank_yes.BringToFront
 img_blank_yes.Tag = "YES"&i
 ScrollView1.Panel.AddView(img_blank_yes, 475, img_height+31, 47dip, 50dip)

next
How would I code the _click sub?
What would make each image unique?
Do I have to load the bitmap each time in the loop?

Thanks in advance

Tom
 
Last edited:

klaus

Expert
Licensed User
Longtime User
Sorry, but I don't understand what exactly you want to do, could you please explain it more precisely or post your project as a zip file.

In your code:
- What view type is img_blank_yes ? An ImageView or a Panel or is there an ImageView on a Panel in each row ?
- You must Dim img_blank_yes inside the loop to initiate a new instance of the view in each row.
- In your line ScrollView1.Panel.AddView(img_blank_yes, 475, img_height+31, 47dip, 50dip) all images are superposed, is this what you want ?
- You should always use use dip values. ScrollView1.Panel.AddView(img_blank_yes, 475dip, img_height+31dip, 47dip, 50dip)
- Do you add other data in the ScrollView ? If yes where and how ?
- Where do you define img_height ?
etc.

With more information I could give you concrete advices.

Best regards.
 
Upvote 0

thughesimsuk

Member
Licensed User
Longtime User
Thanks for the reply Klaus.

Maybe this will make more sense,

B4X:
Sub populate_questions
   
   Dim Cursor1 As Cursor
      Cursor1 = SQL1.ExecQuery("SELECT * FROM FireSafety")

   For i = 0 To Cursor1.RowCount -1 
   
      
      img1.Initialize("ImgView")
      Cursor1.Position = i
      
      img1.Bitmap = LoadBitmap(File.DirAssets, "audit_template_new.png")
      ScrollView1.Panel.AddView(img1, 40dip, img_height, 728dip, 115dip)
   
         'QuestionRef
         lbl_questionref.Initialize("Label")
         lbl_questionref.Text = Cursor1.GetString("QuestionNr")
         lbl_questionref.TextColor = Colors.Black
         ScrollView1.Panel.AddView(lbl_questionref, 50dip, img_height+10dip, 350dip, 40dip)
         
         'Question Content
         lbl_question.Initialize("Label")
         lbl_question.Text =  Cursor1.GetString("Question")
         lbl_question.TextColor = Colors.DarkGray
         ScrollView1.Panel.AddView(lbl_question, 50dip, img_height+40dip, 380dip, 70dip)      
         
         'Image Blank         
         img_blank_yes.Initialize("YES")
         img_blank_yes.Bitmap = LoadBitmap(File.DirAssets, "yes_blank.png")
         img_blank_yes.BringToFront
         img_blank_yes.Tag = i
         ScrollView1.Panel.AddView(img_blank_yes, 475dip, img_height+31dip, 47dip, 50dip)
         
         'Add 115 to add to the TOP value
         img_height = img_height + 115dip      
   Next
   
   Cursor1.Close
   
   ScrollView1.Panel.Height = img_height

End Sub
Sub YES_Click
   Msgbox(img_blank_yes.Tag,"Image")
end sub

So when I call the 'YES_click' sub I want to retrieve the tag OR some kind of id.

Hope this helps and thankyou for spending your time to help me.

Tom
 
Upvote 0

thughesimsuk

Member
Licensed User
Longtime User
Sender

I found out about something called 'sender' which I used in my click sub call, this has given me the unique identifier for all of my images.

:sign0104:
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Try this code :
B4X:
Sub populate_questions
    Dim Cursor1 As Cursor
    
    Cursor1 = SQL1.ExecQuery("SELECT * FROM FireSafety")

    For i = 0 To Cursor1.RowCount -1 
        Cursor1.Position = i

        Dim img1 As ImageView
        img1.Initialize("ImgView")
        img1.Bitmap = LoadBitmap(File.DirAssets, "audit_template_new.png")
        ScrollView1.Panel.AddView(img1, 40dip, img_height, 728dip, 115dip)
    
        'QuestionRef
        Dim lbl_questionref As Label
        lbl_questionref.Initialize("Label")
        lbl_questionref.Text = Cursor1.GetString("QuestionNr")
        lbl_questionref.TextColor = Colors.Black
        ScrollView1.Panel.AddView(lbl_questionref, 50dip, img_height+10dip, 350dip, 40dip)
            
        'Question Content
        Dim lbl_question As Label
        lbl_question.Initialize("Label")
        lbl_question.Text =  Cursor1.GetString("Question")
        lbl_question.TextColor = Colors.DarkGray
        ScrollView1.Panel.AddView(lbl_question, 50dip, img_height+40dip, 380dip, 70dip)        
            
        'Image Blank  
        Dim img_blank_yes As ImageView
        img_blank_yes.Initialize("YES")
        img_blank_yes.Bitmap = LoadBitmap(File.DirAssets, "yes_blank.png")
        img_blank_yes.BringToFront
        img_blank_yes.Tag = i
        ScrollView1.Panel.AddView(img_blank_yes, 475dip, img_height+31dip, 47dip, 50dip)
            
        'Add 115 to add to the TOP value
        img_height = img_height + 115dip        
    Next
    
    Cursor1.Close
    
    ScrollView1.Panel.Height = img_height

End Sub

Sub YES_Click
    Dim img As ImageView
    
    img = Sender
    Msgbox(img.Tag,"Image")
End Sub
As already said in my previous post you must Dim all the views you want to add to the ScrollView1.Panel inside the For / Next loop.
If you want to change the image of the ImageView in the YES_Click event use img.Bitmap = xxxxx

Best regards.
 
Upvote 0

thughesimsuk

Member
Licensed User
Longtime User
Thanks

Thanks again Klaus! :sign0098:


Try this code :
B4X:
Sub populate_questions
    Dim Cursor1 As Cursor
    
    Cursor1 = SQL1.ExecQuery("SELECT * FROM FireSafety")

    For i = 0 To Cursor1.RowCount -1 
        Cursor1.Position = i

        Dim img1 As ImageView
        img1.Initialize("ImgView")
        img1.Bitmap = LoadBitmap(File.DirAssets, "audit_template_new.png")
        ScrollView1.Panel.AddView(img1, 40dip, img_height, 728dip, 115dip)
    
        'QuestionRef
        Dim lbl_questionref As Label
        lbl_questionref.Initialize("Label")
        lbl_questionref.Text = Cursor1.GetString("QuestionNr")
        lbl_questionref.TextColor = Colors.Black
        ScrollView1.Panel.AddView(lbl_questionref, 50dip, img_height+10dip, 350dip, 40dip)
            
        'Question Content
        Dim lbl_question As Label
        lbl_question.Initialize("Label")
        lbl_question.Text =  Cursor1.GetString("Question")
        lbl_question.TextColor = Colors.DarkGray
        ScrollView1.Panel.AddView(lbl_question, 50dip, img_height+40dip, 380dip, 70dip)        
            
        'Image Blank  
        Dim img_blank_yes As ImageView
        img_blank_yes.Initialize("YES")
        img_blank_yes.Bitmap = LoadBitmap(File.DirAssets, "yes_blank.png")
        img_blank_yes.BringToFront
        img_blank_yes.Tag = i
        ScrollView1.Panel.AddView(img_blank_yes, 475dip, img_height+31dip, 47dip, 50dip)
            
        'Add 115 to add to the TOP value
        img_height = img_height + 115dip        
    Next
    
    Cursor1.Close
    
    ScrollView1.Panel.Height = img_height

End Sub

Sub YES_Click
    Dim img As ImageView
    
    img = Sender
    Msgbox(img.Tag,"Image")
End Sub
As already said in my previous post you must Dim all the views you want to add to the ScrollView1.Panel inside the For / Next loop.
If you want to change the image of the ImageView in the YES_Click event use img.Bitmap = xxxxx

Best regards.
 
Upvote 0
Top