Formatting rows of a webview

Ricky D

Well-Known Member
Licensed User
Longtime User
I have tables that have 20+ fields and want to be able to scroll horizontal & vertical.

It seems to me that a WebView does this.

One of my tables is Fares

TableID INT, ShiftTableID INT, FareDate INT .......

How can I format the output so I see the date instead of something like 1209876502 and also format doubles to be .00 ie. 45.97 or 56.00 etc
Also to be able to show booleans as Yes/No

While I have extensive experience in VB6 and other languages Android is very new to me. I don't have any html experience so any help is much appreciated!

regards, Ricky
 

junaidahmed

Well-Known Member
Licensed User
Longtime User
Thanks for your quick reply.but I already know ExecuteMemoryTable for getting cell value.but my requirement is to get cell value from html table via ASP script not sqlite .See the below code and advise.

Webview1.Loadurl("http://202.89.74.150/Myweb/Data.asp")
 
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
oh ok.

I don't know anything about asp scripts. :sign0013:

regards, Ricky
 
Upvote 0

junaidahmed

Well-Known Member
Licensed User
Longtime User
As per Example Code I have run the application for getting cell value from HTML Table using Webview.It's show an error message "Sub CellClickHandler doest not match expected signature"

Sub Process_Globals
Dim WebViewExtras1 As WebViewExtras
End Sub
Sub Activity_Create(FirstTime As Boolean)
WebViewExtras1.addJavascriptInterface(webView1, "B4A")
End Sub
Sub CellClickHandler(ColumnStr As String, RowStr As String)
Dim Column, Row As Int
Column=ColumnStr
Row=RowStr
End Sub

Code for HTML Table
<!-- an example link from column 4, row 1 of your table --><td><a href="javascript:void(0)" onclick="B4A.CallSub('CellClickHandler', '4', '1')">08:02 PM</a></td>
 

Attachments

  • Untitled.jpg
    Untitled.jpg
    35 KB · Views: 293
Upvote 0

warwound

Expert
Licensed User
Longtime User
It looks to me as if you have mixed up the syntax from my older JSInterface library with the syntax of the newer WebViewExtras.

Take a look at my documentation: http://www.b4x.com/forum/additional-libraries-official-updates/12453-webviewextras.html#post70053

CallSub(subName As String, callUIThread As boolean, parameter1 As String, parameter2 As String)

The callUIThread parameter is an important update - it's not available with JSInterface.

Does the Sub called by your javascript modify your activity UI?
If the answer is yes then you need to pass boolean true as callUIThread otherwise you can pass false.
If you pass false and then the Sub tries to modify your activity UI you will get an exception.

Does your javascript require a return value from your Sub?
If the answer is yes then the Sub MUST NOT modify the activity UI.
If CallSub is excuted with callUIThread set to true then no values will be returned from your Sub to the javascript.

You will need to structure your B4A code so that Subs that return values to javascript do not modify the activity UI.

Take your posted snippet of HTML

Code for HTML Table
<!-- an example link from column 4, row 1 of your table --><td><a href="javascript:void(0)" onclick="B4A.CallSub('CellClickHandler', '4', '1')">08:02 PM</a></td>

That should be:

B4X:
<td><a href="javascript:void(0)" onclick="B4A.CallSub('CellClickHandler', false, '4', '1')">08:02 PM</a></td>

OR

B4X:
<td><a href="javascript:void(0)" onclick="B4A.CallSub('CellClickHandler', true, '4', '1')">08:02 PM</a></td>

The value for callUIThread depends upon what action your B4A Sub CellClickHandler takes - if the Sub makes any changes to your Activity UI then you should use true, otherwise false is ok.

Martin.
 
Upvote 0

junaidahmed

Well-Known Member
Licensed User
Longtime User
B4A.CallSub('CellClickHandler', false, '4', '1')"
is fine for getting cell value from HTML Table,Now I have clear for this problem but I would like to know how to hide anchor i.e Value should display without underline.Is there any properties for hiding anchor link (underline) ? Pls advise ASAP.
 
Upvote 0

junaidahmed

Well-Known Member
Licensed User
Longtime User
Sorry for the above comments I have missed some points.

<td><a href="javascript:void(0)" onclick="B4A.CallSub('CellClickHandler', true, '4', '1')">08:02 PM</a></td>is fine for getting cell value from HTML Table,Now I have clear for this problem but I would like to know how to hide anchor i.e Value should display without underline.Is there any properties for hiding anchor link (underline) ? Pls advise ASAP.

Example :-
08:02 PM should display as 08:02 PM
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
You need to add some CSS styles to your webpage to change the default anchor style.

https://www.google.co.uk/search?q=css+style+anchor&ie=UTF-8&oe=UTF-8

Something like this should work (the entire CSS style should be declared in the <head> section of the webpage):

B4X:
<style type="text/css">
a {
 text-decoration:none;
}
</style>

You will probably find that, by default, a browser/WebView will also change the text color of an anchor, so you can override that default behavior with a little more CSS:

B4X:
<style type="text/css">
a {
 color:black; /* or whatever color you want */
 text-decoration:none;
}
</style>

Once an anchor has been clicked it may change color, have a look at the Google link i posted for more detailed info, or post again and i'll try to help.

Martin.
 
Upvote 0

MaaMoz

Member
Licensed User
Longtime User
WebViewExtras B4A.CallSub does not work with Android 2.3.6 API Level 10

Here is the entire code for one of my activities.


------------------ END Quote ------------------------

Ricky D states in his Post that he is using Android 2.3.6 and WebViewExtras B4A.CallSub does NOT work.

I have the same problem: Using Galaxy Tab 7 updated to Android 2.3.6 and WebViewExtras B4A.CallSub does NOT work.

Have tried a 100 things... it will never work.

WebViewExtras B4A.CallSub is my preferred tool... warwound... what is the solution?
You did not reply to Ricky D.... no solution yet?

Urgently need B4A.CallSub !!
 
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
@maamoz I now got it working

Here is how I build my lines :

B4X:
Sub ExecuteHtml(Limit As Int) As String
   If Limit > 0 Then Limit = Min(Limit, listAddMenuItems.Size) Else Limit = listAddMenuItems.Size 
   Dim sb As StringBuilder
   sb.Initialize
   sb.Append("<html><body>").Append(CRLF)
   sb.Append("<style type='text/css'>").Append(HtmlCSS).Append("</style>").Append(CRLF)
   sb.Append("<table><tr>").Append(CRLF)
   'Create the header line
   For i = 0 To 1
      Select i
         Case 0   'Id
            sb.Append("<th>").Append("Id").Append("</th>")
         Case 1   'MenuItem
            sb.Append("<th>").Append("Menu Item").Append("</th>")
      End Select
   Next
   sb.Append("</tr>").Append(CRLF)
   
   'Create the rows
   For row = 0 To Limit - 1
      Dim Record() As String
      Record = listAddMenuItems.Get(row)
      If row Mod 2 = 0 Then
         sb.Append("<tr>")
      Else
         sb.Append("<tr class='odd'>")
      End If
      For i = 0 To Record.Length - 1
         sb.Append("<td>")
         sb.Append(u.BuildHTMLField(row, i, Record(i)))
         'sb.Append("<a href='http://").Append(i).Append(".")
         'sb.Append(row)
         'sb.Append(".com'>").Append(Record(i)).Append("</a>")

         sb.Append("</td>")
      Next
      sb.Append("</tr>").Append(CRLF)
   Next
   sb.Append("</table></body></html>")
   
   Log(sb.ToString)
   Return sb.ToString
End Sub

which calls this

B4X:
Sub BuildHTMLField(row As Int, col As Int, value As String) As String
   Dim sb As StringBuilder 
   sb.Initialize 

   sb.Append("<a href=" & QUOTE & "javascript:void(0)" & QUOTE).Append("onclick=" & QUOTE & "B4A.CallSub('CellClickHandler', true, " & row & "," & col).Append(")").Append(QUOTE)
   sb.Append(">").Append(value).Append("</a>")
   
   Return sb.ToString
End Sub

the cell click handler is

B4X:
Sub CellClickHandler(RowStr As String, ColStr As String)
   Dim Row, Col As Int
   
   Row = RowStr
   Col= ColStr
   
   Dim TableID As Int
   Dim Record() As String
   
   Record = listAddMenuItems.Get(Row)
   TableID = Record(0)
   
   Dim result As Int

   result = Msgbox2("You have selected Add Menu Item " & Record(1),"Choose","Edit","Delete","Cancel",Null)
   
   Dim s As String 
   
   Select result
      Case DialogResponse.POSITIVE
         addmenuitem.Initialize(Record(0), Record(1), False)
         
         StartActivity(AddMenuEdit)
         
      Case DialogResponse.CANCEL
         result = Msgbox2("Are you sure you want to Delete Menu Item " & Record(1) & "?","Confirm Delete","Yes","","No",Null)
         If result = DialogResponse.POSITIVE Then
            s = "DELETE FROM AddMenuItems WHERE Id=" & TableID
            u.mySQL.ExecNonQuery(s)
            LoadAddMenuItems
         End If
         
   End Select
   
   
End Sub

If you have any more questions then please do ask.

I thought that I'd never get it working but just last week after 2 months of forgetting about it I came up with the BuildHTMLField sub.

I found the issue wasn't WebViewExtras but the exact requirement by java to have double and single quotes in a string.

cheers, Ricky
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Ricky D states in his Post that he is using Android 2.3.6 and WebViewExtras B4A.CallSub does NOT work.

I have the same problem: Using Galaxy Tab 7 updated to Android 2.3.6 and WebViewExtras B4A.CallSub does NOT work.

Have tried a 100 things... it will never work.

WebViewExtras B4A.CallSub is my preferred tool... warwound... what is the solution?
You did not reply to Ricky D.... no solution yet?

Urgently need B4A.CallSub !!

I've replied in to your original question here.

Martin.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Hi Rick.

You're right, i didn't document WebViewExtras and create any examples.
I was pushed for time when i created that library so basically just uploaded it with minimal information.

Anyway it looks as though the callUIThread parameter is where you are having problems.

Do you understand the concept of a java Thread?
The JavascriptInterface runs in it's own Thread, it's code executes in a different context to your B4A code.
Your B4A code runs in what is known as the user interface (UI) Thread.
Your B4A user interface can only be modified by code that executes in the UI thread.

JSInterface didn't take any account of this and if javascript used the CallSub method and that CallSub tried to modify the B4A user interface an exception would occur.
So with WebViewExtras i added the callUIThread parameter to the CallSub method.

Think of it this way - does your javascript CallSub cause the B4A user interface to be modified?
If so then you must set callUIThread to true, otherwise set callUIThread to false.

That's the theory, here's the solution (assuming that your B4A Sub HTMLsub does modify the user interface):

B4X:
Javascript = "B4A.CallSub('HTMLsub', true, document.documentElement.outerHTML)"

Martin.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
The form on that page has an id and the input field has a name so you can use this code to set the value of the input field:

B4X:
The forum won't allow me to post the code - the javascript in the code is detected by the forum as malicious!
See attached files.

As my comment says, you can also submit the form from javascript but that creates an endless page reload loop - if you want to submit the form using javascript you'd have to use some logic to avoid such a loop.

Martin.
 

Attachments

  • WebViewFormInput.zip
    6.1 KB · Views: 351
Upvote 0

warwound

Expert
Licensed User
Longtime User
Wow, thank you very much! This really helps me a lot.
I tried your zip and it works excellent.

May I ask you for one more favour: What code should I add to automatically press the "Zoeken" button on that webpage?
Thank you in advance.

The "Zoeken" button is the form's submit button - so uncomment the line:

B4X:
Javascript.Append("document.forms.masterform.submit()")

And the form will be submitted by the javascript - but the page submits to itself...
The form will submit, the page will reload (with the results of the search) BUT my example code will then run again when the Page_Finished event is raised Url="http://www.aangepastlezen.nl/home/boeken".
You'll get an endless loop as the page initially loads, populates the input field, submits itself and reloads - the Page_Finished Sub will again populate the input and submit the form.

You can fix this...
Somewhere in your code you are generating the text to search for from speech input.
So when you have text to search for you can populate the input field with that text and execute the javascript that will submit the form.
There will be no endless loop if you don't automatically populate the input field and submit the form each time the web page is loaded.

Martin.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Hi Rick.

The problem with that page is that the form id contains a hyphen, that causes problems when referencing the form using dot notation:

PHP:
document.forms.user-login.name.value='[email protected]';

This causes "ReferenceError: invalid assignment left-hand side", as the hypen is interpreted a mathematical minus.
Ths solution is to use an alternative syntax to reference the form:

PHP:
document.forms['user-login'].name.value='[email protected]';

As before the forum software prevents me from including the new code in this post so download the attached project.

I've added a flag to prevent the code from going into an endless loop submitting itself to itself, and some javascript you could use to check the 'Mijn wachtwoord onthouden' checkbox if desired.

Martin.
 

Attachments

  • WebViewLoginFormSyntax.zip
    6.1 KB · Views: 262
Upvote 0
Top