android从活动中获取数据并以html显示

时间:2013-03-21 03:15:59

标签: android html webview parameter-passing

我想从我的活动中添加变量并将其显示在html中。下面是我如何将变量插入到webview中,但我不确定如何从html获取数据并以“textview”形式显示?

修改

爪哇

    webView = (WebView)findViewById(R.id.webView);
    webView.setWebViewClient(new WebViewClient());
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView.getSettings().setDatabaseEnabled(true);
    webView.getSettings().setDatabasePath("/data/data/com.example.hybrid/hybrid_user");
    webView.loadUrl("file:///android_asset/www/index.html");
    webView.loadUrl("javascript:callFromActivity(\""+userid+"\")");

HTML

<body>
<form>
    <fieldset>
        <legend>Hybrid Application</legend>
        <p>
            <label>UserName: </label> 
        </p>
        <p id="mytext">Hello!</p>
        <p>
            <label>Mobile Number :</label>
            <input type = "text"
                        id = "mobileNum" />
        </p>
            <button type="button" 
            onclick="updateRecord()">Submit</button>    
                <div id="output"></div> 
    </fieldset>
</form> 

 <script>
    function errorHandler(transaction, error){
        alert('Error:'+error.message+' (Code '+error.code+')');
        return true;
    }
    window.onerror = errorHandler;
    var shortName = 'hybrid_user';
    var version = '1.0';
    var displayName = 'Hybrid Database';
    var maxSize = 65536;
    var db = openDatabase(shortName, version, displayName, maxSize);

    function updateRecord(id, textE1){
        database.transaction(function(tx){
            tx.executeSql("UPDATE hybrid_user SET mobile = ? WHERE userid = ?", [textE1.innerHTML, id], null, onError);
        })
    }

    function callFromActivity(msg){
        document.getElementById("mytext").innerHTML = msg;          
    }
</script>

我是html的新手,我不知道我的代码有什么问题,它没有显示html页面中的userid。任何意见和答案将不胜感激!

修改更新

 webView.loadUrl("javascript:callFromActivity(\""+userid+"\")");

显然系统没有通过这行代码。无论如何都要通过这段代码吗?

2 个答案:

答案 0 :(得分:0)

尝试更改innerHTML以获取如下值:

function callFromActivity(msg) {
   document.getElementById("mytext").value = msg;          
}

答案 1 :(得分:0)

    webView.setWebViewClient(new WebViewClient(){

        @Override
        public void onPageFinished(WebView view, String url){
            webView.loadUrl("javascript:callFromActivity(\""+userid+"\")");
        }
    });

我解决了这个问题。当页面加载完成后,它将执行上面的代码并从活动中获取userid