2
在我的項目中,首先,我從數據庫中獲得了幾個字符串,然後我將每個字符串標記爲<li>
並將它們追加到空的<ul>
調用javascript函數。每當我修改代碼並運行它,第一次,js函數似乎沒有被執行,但是當我再次運行它時,js工作,所有字符串已被添加到<ul>
,作爲<li>
。只要我從不修改代碼,應用程序工作正常,這意味着如果我重新安裝它在移動js不起作用,直到第二次打開此應用程序。數據庫沒有什麼問題。我認爲問題是web視圖在js執行之前被繪製,然後當webview被重新繪製時,一切都很順利。在第一次運行應用程序時,執行javascript與WebView.loadUrl不起作用
這裏是我的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();
}
}
}
}
這裏的js函數(這是一個jQuery函數實際上):
function Element_AppendHTML(ElementID,HTML)
{
$("#"+ElementID).append(HTML);
}
以下是原始的HTML文件,如果你需要看看:
<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>
任何建議?
你好,你解決了這個問題嗎?我現在正在解決這個問題....不明白爲什麼...... –