单击列表项时打开webview [android]

时间:2014-04-09 07:47:23

标签: android

在下面单击列表的第一项(open1)时打开1.html但是当第二项列表(open10)再次打开时1.html

我想点击第二项清单(open10)打开10.html(等下一项......)

我能做什么?

Main.class:

public class Main extends ListActivity {
private static final String[] items = { "open1","open10"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    setListAdapter(new ArrayAdapter<String>(this, R.layout.row, R.id.label,items));
    ListView lv = (ListView) findViewById(android.R.id.list);

lv.setOnItemClickListener(new OnItemClickListener() {
     public void onItemClick(AdapterView<?> parent, View view,
             int position, long id) {
           String title="file:///android_asset/1.html",des="file:///android_asset/10.html";
             Intent intent = new Intent(getApplicationContext() ,WebViewActivity.class);
             intent.putExtra("TITLE", title);
             intent.putExtra("DES", des);
             startActivity(intent);
         }
       });
}}

WebViewActivity.class:

public class WebViewActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);
    Intent intent = getIntent();
    String title  = intent.getStringExtra("TITLE");
    String  des = intent.getStringExtra("DES");
    WebView webview = (WebView)findViewById(R.id.webView1);
    webview.loadUrl(des);
}}

2 个答案:

答案 0 :(得分:0)

创建一个这样的数组。

String[] mFilepath = new String[] { "file:///android_asset/1.html",
            "file:///android_asset/10.html" };

然后将适当的路径发送到接收器类。

public class Main extends ListActivity {

private static final String[] items = { "open1","open10"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ListView lv = (ListView) findViewById(android.R.id.list);
    setListAdapter(new ArrayAdapter<String>(this, R.layout.row, R.id.label,items));


lv.setOnItemClickListener(new OnItemClickListener() {
     public void onItemClick(AdapterView<?> parent, View view,
             int position, long id) {

             Intent intent = new Intent(getApplicationContext() ,WebViewActivity.class);
             intent.putExtra("TITLE", title);
             intent.putExtra("DES", mFilepath[position]);
             startActivity(intent);
         }
       });
}}

并在WebViewActivity类中加载此URL。请随时询问是否有任何问题。

答案 1 :(得分:0)

使用此代码从列表项打开新的WebView:Position用于启动WebView来自ListView

的每个项目
list_Id.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View v,
                                        int position, long id) {
                    list_Id.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View v,
                                                int position, long id) {
                            WebView webView = new WebView(v.getContext());
                            String[] urls = getResources().getStringArray(R.array.bookmark_urls);
                            webView.loadUrl(urls[position]);
                        }
                    });
                }
            });

string-array.xml中创建values文件,并将<item>的所有网址设为<resources>,如下所示:

<resources>
    <string-array name="bookmark_urls">
        <item>http://www.google.com</item>
        <item>http://www.android.com/</item>
        <item>http://www.facebook.com/</item>
        <item>http://www.android.com/</item>
        <item>http://www.android.com/</item>
        <item>http://www.android.com/</item>
        <item>http://www.android.com/</item>
        <item>http://www.android.com/</item>
        <item>http://www.android.com/</item>
        <item>http://www.android.com/</item>
    </string-array>
</resources>

这非常有效。