Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Thanks @CommonsWare for the comment, i tracked the error and got this:</p> <p><em>"android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views."</em></p> <p>Googled it and got <a href="https://stackoverflow.com/questions/1157814/how-to-access-original-activitys-views-from-spawned-background-service">this forum thread</a></p> <p>So, in order to update a view in the UI at runtime and outside it's thread, you have to use a Handler, that receive messages and execute actions accordingly.</p> <p><strong>So i implemented a Handler for the Activity, that gets a message with an int identifier and the data i want to update into the webview:</strong></p> <pre><code>final Handler handler = new Handler() { public void handleMessage(Message msg) { int cod = msg.getData().getInt("messageType"); switch(cod){ case 1: String date = msg.getData().getString("datePickerValue"); mWebView.loadUrl("javascript:setReturnDate('" + mId + "', '" + date + "');"); break; case 2: showDialog(0); break; } } }; </code></pre> <p><strong>I had to change the openDatePicker method of the class to use the Handler, as well as create an input parameter, which is the id of the control in the page:</strong></p> <pre><code>public void openDatePicker(String id){ mId = id; //mId is a string declared in the Activity scope, so it can be used anywhere Message msg = new Message(); Bundle b = new Bundle(); b.putInt("messageType", 2); msg.setData(b); handler.sendMessage(msg); } </code></pre> <p><strong>Rewrote the DatePicker listener for the date selection, to get the data and send a message to the Handler:</strong></p> <pre><code>private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Message msg = new Message(); Bundle b = new Bundle(); b.putInt("messageType", 1); b.putString("datePickerValue", String.format("%02d", dayOfMonth) + "/" + String.format("%02d", (monthOfYear + 1)) + "/" + String.valueOf(year)); msg.setData(b); handler.sendMessage(msg); } }; </code></pre> <p><strong>In the webpage, the js functions got out like this:</strong></p> <pre><code>function openDatePicker(id) { if (objAndroid != null) objAndroid.openDatePicker(id); } function setReturnDate(id, date) { var obj = document.getElementById(id); if (obj != null) obj.value = date; } </code></pre> <p><strong>And finally, the input (TextBox), got like this:</strong></p> <pre><code>TextBox obj = FindControl("txtDt") as TextBox; if (obj != null) { obj.Attributes.Add("readonly", "readonly"); obj.Attributes.Add("OnClick", "javascript:openDatePicker(this.id)"); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload