在listview项目onclick上加载html

时间:2010-10-18 06:30:08

标签: html android listview webview

我现在停留在我的资源文件夹中的html加载中。我在资源文件夹下有几个html页面,必须在listview项目onclick上加载它们。每个listview项目都拥有自己的html页面。有谁知道如何获得onclick事件以及如何显示特定的html页面?

由于

4 个答案:

答案 0 :(得分:1)

  1. 您可以使用ListView的setOnItemClickListener方法获取列表项上的点击事件。
  2. 您可以使用WebView的loadUrl方法来显示您的html页面。见WebView tutorial

答案 1 :(得分:1)

老兄......放弃......它不起作用 - 当你在listview中有一个webview时,没办法让这个监听器工作

答案 2 :(得分: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);                                                          

                    }                                                                                      

            }                                                                                              
        });                                                                                                


    }                                                                                                      
}                                                                                                

这是WebViewDemo。我只是扩展了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;
        }

}

答案 3 :(得分:0)

您可以使用自定义webViewClient并捕获html中的链接,然后您可以按照所需的点击进行点击。