通过ListView使用webView打开多个本地html文件 [英] Opening multiple local html files using webView via ListView
本文介绍了通过ListView使用webView打开多个本地html文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个ListView和两个html文件。当我点击我的第一个列表项时,它将我引导至HTML1文件。但是,当我点击第二个列表项目时,它仍然将我指向相同的HTML1文件而不是第二个HTML文件。
如果我点击第二个List Item,我该如何转到第二个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,Android中的简单列表视图,
创建列表视图Android示例,Android示例,
List View Source Code,List View Array Adapter,
Android示例列表视图};
ArrayAdapter< String> adapter = new ArrayAdapter< String>(this,
android.R.layout.simple_list_item_1,android.R.id.text1,values);
//将适配器分配给ListView
listView.setAdapter(adapter);
// ListView Item Click Listener
listView.setOnItemClickListener(new OnItemClickListener(){
$ b @Override
public void onItemClick(AdapterView<?>父视图,视图视图,
int position,long id){
// TODO自动生成的方法存根
// ListView单击的项目索引
int itemPosition = position;
// ListView单击项目值
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(),
S tory.class);
startActivity(myIntent);
}
//显示提醒
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 {b
$ b 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);
$ b公共类myWebClient扩展WebViewClient {
@Override
public void onPageStarted(WebView视图,字符串url,位图favicon ){
// TODO自动生成的方法存根
super.onPageStarted(view,url,favicon);
$ b @Override
public boolean shouldOverrideUrlLoading(WebView view,String url){
// TODO自动生成的方法存根
view.loadUrl(URL);
返回true;
$ @覆盖
public void onReceivedError(WebView视图,int errorCode,
字符串描述,String failingUrl){
}
@Override
public void onPageFinished(WebView视图,String url){
// TODO自动生成的方法存根
super.onPageFinished(view,url);
$ b $ / code $ / pre>
}
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:paddingRight =@ dimen / activity_horizontal_margin
android:paddingLeft =@ dimen / activity_horizontal_margin
android:paddingRight =@ dimen / activity_horizontal_marginb:android:layout_height =match_parent
android:paddingBottom =
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
/>
解决方案您忘记使用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);
}
I have a ListView and two html files. When I click on my first list item it directs me to the HTML1 file . But when I click on second list item, it still directs me to the same HTML1 file instead of the second HTML file.
How do I go to second html file if I click on second List Item?
Here's my code:
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"
/>
解决方案 You forgot to use 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);
}
这篇关于通过ListView使用webView打开多个本地html文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文