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

时间:2014-10-07 20:20:24

标签: android html listview webview

我有一个ListView和两个html文件。当我点击我的第一个列表项时,它会将我引导到HTML1文件。但是当我点击第二个列表项时,它仍然指示我使用相同的HTML1文件而不是第二个HTML文件。 如果单击第二个列表项,如何进入第二个html文件?

这是我的代码:

MainActivity.java

public class MainActivity extends Activity {
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById(R.id.list);


        String[] values = new String[] { "Story1",
                "Test1", "Simple List View In Android",
                "Create List View Android", "Android Example",
                "List View Source Code", "List View Array Adapter",
                "Android Example List View" };

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, values);

        // Assign adapter to ListView
        listView.setAdapter(adapter);

        // ListView Item Click Listener
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                // ListView Clicked item index
                int itemPosition = position;

                // ListView Clicked item value
                String itemValue = (String) listView
                        .getItemAtPosition(position);
                if (position == 0) {
                    Intent myIntent = new Intent(getApplicationContext(),
                            Story.class);
                    startActivity(myIntent);
                }else if (position == 1) {
                    Intent myIntent = new Intent(getApplicationContext(),
                            Story.class);
                    startActivity(myIntent);
                }

                // Show Alert
                Toast.makeText(
                        getApplicationContext(),
                        "Position :" + itemPosition + "  ListItem : "
                                + itemValue, Toast.LENGTH_LONG).show();

            }

        });
    }
    }

activity_main.xml中

<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>

Story.java

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);

    }
}

}

activity_story

<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"
    />

1 个答案:

答案 0 :(得分:0)

你忘了使用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);
        }