Android 通过ListView使用webView打开多个本地html文件

Android 通过ListView使用webView打开多个本地html文件,android,html,listview,webview,Android,Html,Listview,Webview,我有一个列表视图和两个html文件。当我单击第一个列表项时,它会将我指向HTML1文件。但当我单击第二个列表项时,它仍然会将我指向同一个HTML1文件,而不是第二个HTML文件。 如果单击第二个列表项,如何转到第二个html文件 这是我的密码: MainActivity.java } 活动与故事 您忘记使用puExtra if (position == 0) { Intent myIntent = new Intent(getApplicationCo

我有一个列表视图和两个html文件。当我单击第一个列表项时,它会将我指向HTML1文件。但当我单击第二个列表项时,它仍然会将我指向同一个HTML1文件,而不是第二个HTML文件。 如果单击第二个列表项,如何转到第二个html文件

这是我的密码:

MainActivity.java

}

活动与故事



您忘记使用puExtra

        if (position == 0) {
            Intent myIntent = new Intent(getApplicationContext(),
                    Story.class);
            myIntent.putExtra("key", 0);
            startActivity(myIntent);
        }else if (position == 1) {
            Intent myIntent = new Intent(getApplicationContext(),
                    Story.class);
            myIntent.putExtra("key", 1);
            startActivity(myIntent);
        }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.listviewandroidexample.MainActivity" >

<ListView
          android:id="@+id/list"
          android:layout_height="wrap_content"
          android:layout_width="match_parent">
</ListView>
public class Story extends Activity {

WebView web;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_story);
    web = (WebView) findViewById(R.id.webView1);
    web.setWebViewClient(new myWebClient());
    web.getSettings().setJavaScriptEnabled(true);
    int pos = getIntent().getIntExtra("key", 0);
    if (pos == 0) {
        web.loadUrl("file:///android_asset/test.html");
    } else if (pos == 1) {
        web.loadUrl("file:///android_asset/test2.html");
    } else if (pos == 2) {
        web.loadUrl("file:///android_asset/work2.html");
    } else if (pos == 3) {
        web.loadUrl("file:///android_asset/work3.html");
    }

}

public class myWebClient extends WebViewClient {
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub

        view.loadUrl(url);
        return true;

    }

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

    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);

    }
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.motivationalstories.Story" >

<WebView
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />
        if (position == 0) {
            Intent myIntent = new Intent(getApplicationContext(),
                    Story.class);
            myIntent.putExtra("key", 0);
            startActivity(myIntent);
        }else if (position == 1) {
            Intent myIntent = new Intent(getApplicationContext(),
                    Story.class);
            myIntent.putExtra("key", 1);
            startActivity(myIntent);
        }