Hello, I've been working on setting an html page using webView from which I need to extract imageClick events and send the ID of image clicked to a b4a sub.
I've used JSInterface which returns the correct ID when I first run the app. Then, it returns nothing. Html is creation is done by a 'loadMap' routine and webView is added in design mode.
The code is:
Any idea on what goes wrong and routine stops to send me data? Thank you.
SOLVED: I had to dim a new variable IN THE b4aGetId sub, instead of the original coming from the script, as follows:
Dim idnew As String
idnew=id
Msgbox(idnew,"test")
My new testing used webViewExtras as proposed by Erel.
I've used JSInterface which returns the correct ID when I first run the app. Then, it returns nothing. Html is creation is done by a 'loadMap' routine and webView is added in design mode.
The code is:
B4X:
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim myInterface As JSInterface
Dim WebView1 As WebView
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout ("setTablesNew")
myInterface.addJSInterface(WebView1, "B4A")
loadMap
End Sub
Sub loadMap
Dim myHtml As StringBuilder
Dim myHtmlString As String
Dim curTable As String
Dim myImages(4) As String
myImages(0)="table1.jpg"
myImages(1)="table2.jpg"
myImages(2)="table3.jpg"
myImages(3)="stand1.jpg"
myHtml.Initialize
myHtml.Append ("<html>")
myHtml.Append ("<script language='JavaScript'>")
myHtml.Append("function sendCellId(id){")
myHtml.Append("B4A.CallSub('b4aGetID', id);")
myHtml.Append ("}</script>")
myHtml.Append("<body><table border='2'>")
For k=1 To 20
myHtml.Append ("<tr>")
For l=1 To 20
curTable=(k-1)*20+l
myHtml.Append ("<td>")
Dim a As Int
a=Rnd(0,10)
If a<4 Then
myHtml.Append ("<img id='" & curTable & "' onclick='javascript:sendCellId(id);' src='file://"& File.DirDefaultExternal & "/images/" & myImages(a) & "' alt='table' /><br>" & curTable)
End If
myHtml.Append ("</td>")
Next
myHtml.Append ("</tr>")
Next
myHtml.Append ("</table>")
myHtml.Append ("</body></html>")
myHtmlString=myHtml.ToString
WebView1.Color=Colors.White
WebView1.LoadHtml(myHtmlString)
End Sub
Sub b4aGetID(id As String )
Msgbox(id,"test")
End Sub
Any idea on what goes wrong and routine stops to send me data? Thank you.
SOLVED: I had to dim a new variable IN THE b4aGetId sub, instead of the original coming from the script, as follows:
Dim idnew As String
idnew=id
Msgbox(idnew,"test")
My new testing used webViewExtras as proposed by Erel.
Last edited: