问题描述
我在一个项目中工作的地方我有一个项目列表视图谷歌,雅虎,必应等物质点击网页视图,必须装入相应的url.my问题的每个产品,我已创建了一个项目列表视图它,但我需要加载,如果我点击谷歌的所有urls.ie只有一个类web视图必须加载谷歌网页,如果我preSS雅虎必须在同一webview.i加载页面雅虎想通过点击的项目的URL到下一个页面(web视图)
i'm working in a project where i have a listview with items google,yahoo,bing etc.on clicking on each item a webview must be loaded with the corresponding url.my problem is that i have created a listview with items on it but i need only one class for loading all the urls.ie if i click on google the webview must load the google page and if i press yahoo it must load the yahoo page in the same webview.i want to pass the url of the clicked item to the next page(webview)
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateListView();
clickEvent();
}
private void populateListView() {
String[] str={"Google","yahoo", "bing"};
ArrayAdapter<String>adapter=new ArrayAdapter<String>(this, R.layout.textview,str);
ListView list=(ListView)findViewById(R.id.listviewone);
adapter.notifyDataSetChanged();
list.setAdapter(adapter);
}
private void clickEvent() {
final ListView list=(ListView)findViewById(R.id.listviewone);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
view.setSelected(true);
switch( position )
{
case 0: Intent newActivity0 = new Intent(MainActivity.this, Mywebpage.class);
startActivity(newActivity0);
break;
}
}
});
}
我webpage.java
my webpage.java
public class Mywebpage extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.webviewpage);
WebView wbView = (WebView) findViewById(R.id.WebView);
wbView.getSettings().setJavaScriptEnabled(true);
wbView.loadUrl("http://www.google.com");
}
}
}}
推荐答案
要发送的标题和网址到的WebView类传球意图额外给的WebView类,即。重写你的开关情况下
To send title and url to webview class pass intent extra to webview class,i.e. Rewrite your switch case as
Intent newActivity0 = new Intent(MainActivity.this, Mywebpage.class);
newActivity0.putExtra("title", str[position]);
switch (position) {
case 0:
newActivity0.putExtra("url", "http://www.google.com");
break;
case 1:
newActivity0.putExtra("url", "http://www.yahoo.com");
break;
case 2:
newActivity0.putExtra("url", "http://www.bing.com");
break;
}
startActivity(newActivity0);
和定义
String[] str={"Google","yahoo", "bing"};
作为类变量,而不是 populateListView
方法。
和在 Mywebpage
类获得意图为
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webviewpage);
Bundle extras = getIntent().getExtras();
String title, url;
if (extras != null) {
title = extras.getString("title");
url = extras.getString("url");
WebView wbView = (WebView) findViewById(R.id.WebView);
wbView.getSettings().setJavaScriptEnabled(true);
wbView.loadUrl(url);
} //rest code
编辑:
请确保您连接到互联网,并已按照清单权限:
Make sure you are connected to internet and you have following permission in the manifest:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
这篇关于通过URL和标题在Android的网页视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!