如何从OnitemCLicklistener调用HTML文件

时间:2013-12-03 18:02:37

标签: android android-webview onitemclicklistener

我希望onitemclicklistener打开存储在assets文件夹中的HTML页面,并且每个活动都应该在WebView中打开,我不希望有这么多的活动和如此多的Web视图

所以这是我的onitemclicklistener

list1.setOnItemClickListener(new OnItemClickListener() {
 public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
           if(position==0)
           {
           Intent myIntent = new Intent(MainActivity.this,Activity1.class);
           startActivity(myIntent);
           } 
          if(position==1)
           {
           Intent myIntent = new Intent(MainActivity.this,Activity2.class);
           startActivity(myIntent);
           } 
          if(position==2)
           {
           Intent myIntent = new Intent(MainActivity.this,Activity3.class);
           startActivity(myIntent);
           } 


}
});
}
}

2 个答案:

答案 0 :(得分:0)

在列表项中单击。

Intent myIntent = new Intent(MainActivity.this,WebViewActivity.class);
myIntent.putExtra("key",position);
startActivity(myIntent);

在WebviewActivtiy中

private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web); // webview in activity_web.xml
int pos = getIntent().getIntExtra("key", 0);
webView = (WebView) findViewById(R.id.wv); // initialize once
webView.setJavaScriptEnabled(true);
if(pos==0)
{ 
webView.loadUrl("file:///android_asset/2.html");
}

答案 1 :(得分:0)

你试试这个。

private WebView YourWebView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.info_web);

    YourWebView = (WebView) findViewById(R.id.YourWebView);


    WebSettings wset = webviewMsg.getSettings();
    wset.setJavaScriptEnabled(true);

    YourWebView.loadUrl(YourURL);

     YourWebView.setWebViewClient(new WebViewClient() {
         @Override
         public boolean shouldOverrideUrlLoading(WebView view, String url) {
         view.loadUrl(url);
         return true;
    }

        @Override
        public void onPageFinished(WebView view, String url) {

        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

        }
    });
}

我希望你能提供帮助。