2

I have a list view with items [google, yahoo, bing ...]. By clicking on each item a webview must be loaded with the corresponding URL. My problem is that I have created a list view with items in it but I need only one class for loading all the URLs. So when I click on google the webview must load the google website and if I press yahoo it must load the yahoo website in the same webview.

I want to pass the URL of the clicked item to the next page (webview).

MainActivity.java

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;

            }              
        }

    });
}

Mywebpage.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");
    }
}
Michal
  • 15,429
  • 10
  • 73
  • 104
Thushara prasad
  • 155
  • 1
  • 2
  • 15
  • 1
    A quick an dirty solution would be to parse the text of the clicked item and do an if else cascade like Sting url; if(text.equals("Google") { url="http://www.google.com"; } else if (text.equals("Yahoo") { url="http://www.yahoo.com"; } else if ... – Forke Nov 07 '14 at 09:40

2 Answers2

3

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

and define

 String[] str={"Google","yahoo", "bing"};

as class variable instead of populateListView method.

and in your Mywebpage class get intent as

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

Edit:

Make sure you are connected to internet and you have following permission in the manifest:

<uses-permission android:name="android.permission.INTERNET"></uses-permission> 
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
2

Create an array
String urlStrArray= {"http://www.google.com","http://www.yahoo.com","http://www.bing.com"};

and in -

    @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                 view.setSelected(true); 
                       Intent newActivity0 = new Intent(MainActivity.this, Mywebpage.class); 
                       intent.putExtra("url",urlStrArray[position]);
                       startActivity(newActivity0);      
            }
Darpan
  • 5,623
  • 3
  • 48
  • 80
  • try other options.. do they work? if yes then change http to https for google. – Darpan Nov 07 '14 at 10:09
  • if my answer helped, you can 'accept'/ 'upvote' my answer so that it could help others facing same issue. – Darpan Nov 07 '14 at 12:37
  • @Darpan, can you please check out similar problem in this thread and provide a solution.? How to load static html files as items in listview.? – Manikandan Mar 02 '15 at 13:21
  • @Mannii88 Did not get what do you mean by loading static htmls as 'items', And ask a new question and share the link with me. Asking new question in comments is not a good practice. :) – Darpan Mar 02 '15 at 14:31