Reputation: 129
In my project, first, I got several strings from database, then I mark up each string to be a <li>
and append them into a empty <ul>
by calling javascript function. Whenever I modified the code and ran it, first time, the js function didn't seem to be executed, but when I ran it again, js worked, all strings had been added to <ul>
as <li>
. As long as I never modified the code, app worked properly, which means if I re-install it in mobile the js doesn't work untill second time open this app. There's nothing wrong with the database things. I think the problem is the webview is painted before the js executed, then when webview is repainted, all go fine.
Here is my activity.java
public class WebView_testActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView web = (WebView)findViewById(R.id.web_content);
webView_handler(web);
data_handler(web);
Log.d(MyLog.LOG, "end of file");
}
private void webView_handler(WebView web){
web.loadUrl("file:///android_asset/FormPanel.html");
web.getSettings().setJavaScriptEnabled(true);
}
private void data_handler(WebView web){
boolean hasSD = Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED);
//String SDPATH = Environment.getExternalStorageDirectory().getPath();
if(hasSD){
DbHelper data_helper = new DbHelper(this, "/mnt/sdcard/CTIL/QuestionDB/Q1.db");
SQLiteDatabase database = data_helper.getReadableDatabase();
String sql = "SELECT pe.Description FROM tblFormEngine_PoolElement pe " +
"NATURAL JOIN tblFormEngine_FormElement fe WHERE fe.PanelID = 1";
Cursor c = database.rawQuery(sql,null);
if(c.getCount() == 0)
Log.d(MyLog.LOG, "empty");
c.moveToFirst();
if(!c.isAfterLast()){
do{
String description = c.getString(0);
String html = "<li><p>" + description + "</p></li>";
Log.d(MyLog.LOG, html);
//web.loadUrl("javascript:Element_AppendHTML('content',\""+html+"\")");
MyThread mt = new MyThread(web, html);
mt.start();
}while(c.moveToNext());
}
c.close();
database.close();
}
}
private class MyThread extends Thread{
private WebView web;
private String html;
MyThread(WebView web, String html){
this.web = web;
this.html = html;
}
public void run(){
try {
web.loadUrl("javascript:Element_AppendHTML('content',\""+html+"\")");
sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Here's js function(It's a JQuery function actually):
function Element_AppendHTML(ElementID,HTML)
{
$("#"+ElementID).append(HTML);
}
Below is the original HTML file should you need to have a look:
<html>
<head>
<title>Form Panel</title>
<meta charset="utf-8">
<link rel="stylesheet" href="theme/FormPanel_Basic.css">
<script type="text/javascript" src="js/jquery-1.5.1.js"></script>
<script type="text/javascript" src="js/jquery.scrollTo-1.4.2-min.js"></script>
<script type="text/javascript" src="js/jquery.scrollToMe.js"></script>
<script type="text/javascript" src="js/FormPanel.js"></script>
</head>
<body>
<ul id="content" selected="true">
</ul>
</body>
</html>
Any suggestion?
Upvotes: 2
Views: 3227
Reputation: 1341
change from
web.loadUrl("file:///android_asset/FormPanel.html");
web.getSettings().setJavaScriptEnabled(true);
to
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("file:///android_asset/FormPanel.html");
don't you call loadurl after you finish setup?
Upvotes: 2