我有一个ListView和两个html文件。当我点击我的第一个列表项时,它将我引导至HTML1文件。但是,当我点击第二个列表项目时,它仍然将我指向相同的HTML1文件而不是第二个HTML文件。 如果我点击第二个列表项目,我该如何去第二个html文件?通过ListView使用webView打开多个本地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 adapter = new ArrayAdapter(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中
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” >
android:id=”@+id/list”
android:layout_height=”wrap_content”
android:layout_width=”match_parent”>
Story.ja VA
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
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” >
android:id=”@+id/webView1″
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
/>
2014-10-07
Sam2301