1.以上是布局页面!
2.在MainActivity对应的Layout文件中,添加一个WebView控件,用于显示上面的html;3.获取数据源的两个类:public class BeanService { public ListgetBeans() { List list = new ArrayList (); list.add(new Bean(1, "1", "138837416xx", "123")); list.add(new Bean(2, "2", "138837416xx", "456"));// public Bean(Integer id, String name, String phone, String amount) return list; }}
4.修改MainActivity:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); WebView wv = (WebView) findViewById(R.id.wv); wv.loadUrl("file:///android_asset/index.html");// 加载assets文件夹中的index.html wv.getSettings().setJavaScriptEnabled(true);// 设置Javascript可用 wv.addJavascriptInterface(new Jsobj(wv), "contact");// 向WebView中注入名为contact的对象 }private final class Jsobj { private WebView wv; public Jsobj(WebView v) { this.wv = v; } public void call(String phone) {// 拨打电话方法 Intent i = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone)); startActivity(i); } public void showAll() {// 显示所有的数据信息 Listlist = new BeanService().getBeans(); JSONArray arr = new JSONArray(); for (Bean b : list) { JSONObject obj = new JSONObject(); try { obj.put("name", b.getName()); obj.put("amount", b.getAmount()); obj.put("phone", b.getPhone()); arr.put(obj); } catch (JSONException e) { e.printStackTrace(); } } String json = arr.toString(); wv.loadUrl("javascript:show('" + json + "')");// 调用webview中的show(Jsonobj)方法-javascript方法 } }}