0

I'm now stuck in html loading from my assets folder. I've several html pages under assets folder and have to load those at listview item onclick. Each listview item own their html pages.Does anybody know how i can get onclick event and how to show specific html pages ?

Thanks

4 Answers 4

1
public class WebViewWithListActivity extends Activity {                                                    
    private String lv_arr[] = { "Android", "iPhone", "BlackBerry"};                                        
    ListView lv1;                                                                                          
    /** Called when the activity is first created. */                                                      
    @Override                                                                                              
    public void onCreate(Bundle savedInstanceState) {                                                      
        super.onCreate(savedInstanceState);                                                                
        setContentView(R.layout.main);                                                                     
        lv1 = (ListView) findViewById(R.id.listView1);                                                     
        lv1.setAdapter(new ArrayAdapter<String>(this,                                                      
                android.R.layout.simple_list_item_1, lv_arr));                                             
        lv1.setTextFilterEnabled(true);                                                                    
        lv1.setOnItemClickListener(new OnItemClickListener() {                                             

            @Override                                                                                      
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,long id)                  
            {                                                                                              
                    AlertDialog.Builder adb = new AlertDialog.Builder(WebViewWithListActivity.this);       
                    adb.setTitle("Selected item");                                                         
                    adb.setMessage("Selected Item is = "                                                   
                            + lv1.getItemAtPosition(position));                                            
                    adb.setPositiveButton("Ok", null);                                                     
                    Log.i("Selected item is ",(String) lv1.getItemAtPosition(position)+"" );               
                     adb.show();                                                                           

                    //TextView tvUrl = (TextView) view.findViewById(R.id.item2);                           
                    TextView tvUrl=(TextView) findViewById(R.id.item2);                                    
                    if(lv1.getItemAtPosition(position).equals("Android"))                                  
                    {                                                                                      
                                GlobalVariable.SetURL("http://www.google.co.in/");                         
                                Log.i("Global vari : ",GlobalVariable.GetURL());                           
                                Intent i = new Intent(WebViewWithListActivity.this,WebViewDemo.class);     
                                //i.putExtra("http://www.google.co.in/", tvUrl.getText());                 

                                startActivity(i);                                                          

                    }                                                                                      

            }                                                                                              
        });                                                                                                


    }                                                                                                      
}                                                                                                

This is WebViewDemo. I simply extended Activity:

public class WebViewDemo extends Activity{
    private WebView webView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_webview);
        //String url =getIntent().getStringExtra("url");//get url that pass from the other screen
        //Log.i("url ", url+"");
        Log.i("Global vari : ",GlobalVariable.GetURL());

        webView = (WebView)findViewById(R.id.wvDisplay);
        WebSettings webSetting= webView.getSettings(); //create new settings for webView
        webSetting.setJavaScriptEnabled(true); // enabled javascript
        webView.setWebViewClient(new WebViewClient()); //set up webviewclient, this set not to open the default browser when link click
        //Log.i("url ", url+"");
        webView.loadUrl(GlobalVariable.GetURL());//load the web page



    }

}


public class GlobalVariable extends Application{
    private static String url;
    public static String GetURL() 
    {
        return url;
    }
     public static void SetURL(String URL) {
            url = URL;
        }

}
Sign up to request clarification or add additional context in comments.

Comments

1
  1. You can use ListView's setOnItemClickListener method to get click event on list items.
  2. You can use WebView's loadUrl method to show your html pages. See WebView tutorial.

2 Comments

yes i know about that but i have lot of html pages to show in webview. what i mean is that every list item has their own html pages. how can i receive onclick item id? anyway, thanks for your reply.
I don't know if I get it right. When list item is clicked, your listener will be triggered, and you can get the clicked item id in your implementation of AdapterView.OnItemClickListener, can't you?
1

Dude ... give up ... it won't work -- no way to get this listener working when you have a webview inside a listview

1 Comment

Well something that finally worked for me ... remove all android:focusable... or android:clickable ... = "false" stuff in your item view definition ... and for each webview you instantiate do the following : webView.setFocusable(false); webView.setClickable(false)
0

You can use a custom webViewClient and catch links in the html, then you can do what ever you want will the clicks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.