1. [代码]html代码
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function show(josndata){
var jsonobjs = eval(jsondata);
var table = document.getElementById("personTable");
for(var y=0; y<jsonobjs.length; y++){
var tr = table.insertRow(table.rows.length); //添加一行
//添加三列
var td1 = tr.insertCell(0);
var td2 = tr.insertCell(1);
td2.align = "center";
var td3 = tr.insertCell(2);
td3.align = "center";
//设置列内容和属性
td1.innerHTML = jsonobjs[y].id;
td2.innerHTML = jsonobjs[y].name;
td3.innerHTML = "<a href='javascript:itcast.call(""+ jsonobjs[y].mobile+ "")'>"+jsonobjs[y].mobile+"</a>";
}
}
</script>
</head>
<!-- js代码通过webView调用其插件中的java -->
<body onload="javascript:itcast.getContacts();">
<table border="0" width="100%" id="personTable" cellpadding="0">
<tr bgcolor="#E6F3FF">
<td width="20%">编号</td>
<td width="40%" align="center">姓名</td>
<td align="center">电话</td>
</tr>
</table>
<a href="javascript:window.location.reload()">刷新</a>
</body>
</html>
2. [代码]静态赋值
package org.itcast.service;
import java.util.ArrayList;
import java.util.List;
import org.itcast.domain.Contact;
public class ContactService {
public List<Contact> getContacts() throws Exception{
List<Contact> contacts = new ArrayList<Contact>();
contacts.add(new Contact(1,"xx","1254154741"));
contacts.add(new Contact(2,"qq","548745141"));
contacts.add(new Contact(21,"ss","13545874514"));
return contacts;
}
}
3. [代码]实体类
package org.itcast.domain;
public class Contact {
private Integer id;
private String name;
private String moblie;
public Contact() {
super();
}
public Contact(Integer id, String name, String moblie) {
super();
this.id = id;
this.name = name;
this.moblie = moblie;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMoblie() {
return moblie;
}
public void setMoblie(String moblie) {
this.moblie = moblie;
}
}
4. [代码][Java]代码
package org.itcast.ui;
import java.util.List;
import org.itcast.domain.Contact;
import org.itcast.service.ContactService;
import org.json.JSONArray;
import org.json.JSONObject;
http://www.huiyi8.com/hunsha/hanshi/
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
韩式婚纱照片
public class HtmlUIActivity extends Activity {
private WebView webView;
private ContactService service;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
service = new ContactService();
webView =(WebView)this.findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);//打开js功能,使浏览器能使用脚本功能
webView.addJavascriptInterface(new ContactPlugin(), "itcast");//为浏览器安装js插件,其中interfaceName表示是一个类名,调用js时都是interfaceName.xx();
webView.loadUrl("file:///android_asset/index.html");
}
private class ContactPlugin{
public void getContacts() throws Exception{
List<Contact> contacts = service.getContacts();//得到联系人数据
JSONArray array = new JSONArray();
for (Contact contact :contacts) {
JSONObject item = new JSONObject();
item.put("id", contact.getId());
item.put("moblie", contact.getMoblie());
item.put("name", contact.getName());
array.put(item);
}
String json = array.toString();//转成json字符串
Log.i("HtmlUIActivity",json);
webView.loadUrl("javascript:itcast.show('"+json+"')");
}
public void call(String moblie){
Intent intent =new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+moblie));
startActivity(intent);
}
}
}