Listview onitemclick在带有webview活动的assets文件夹中打开html文件

时间:2014-09-23 12:52:09

标签: html listview webview local

我需要列表中的每个项目打开一个html中的页面,但不想要浏览器但是在活动中

webview_layout.xml

    <WebView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/wv"
    tools:context="${packageName}.${activityClass}" >

</WebView>

点击项目列表 - &gt;在资产中加载URL HTML文件

DrinkList.java

    package ptbr.sixlions.recipeweed;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;

public class DrinkList extends Activity {

    private ListView lv;


    private String title[] = { "version: 1.5", "version: 1.6",
            "version: 2.0 & 2.1"};

    private int thumb[] = { R.drawable.drink, R.drawable.drink, R.drawable.drink};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_v);



        // initialize the variables:
        lv = (ListView) findViewById(R.id.listView);

        lv.setAdapter(new VersionAdapter(this));

        lv.setOnItemClickListener(new OnItemClickListener() {

                    @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                int pos = arg2;


                Intent intent = new Intent(DrinkList.this, WebViewL.class);
                intent.putExtra("key", pos);
                startActivity(intent);

                }                   


        });

        }

        class VersionAdapter extends BaseAdapter {

            private LayoutInflater layoutInflater;

            public VersionAdapter(DrinkList drinkList) {
                // TODO Auto-generated constructor stub
                layoutInflater = (LayoutInflater) drinkList
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            }

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return title.length;
            }

            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return position;
            }

            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub

                View listItem = convertView;
                int pos = position;
                if (listItem == null) {
                    listItem = layoutInflater.inflate(R.layout.list_item, null);
                }

                // Initialize the views in the layout
                ImageView iv = (ImageView) listItem.findViewById(R.id.thumb);
                TextView tvTitle = (TextView) listItem.findViewById(R.id.title);

                // set the views in the layout
                iv.setBackgroundResource(thumb[pos]);
                tvTitle.setText(title[pos]);

                return listItem;
            }

        }

    }

WebView活动

WebViewL.java

package ptbr.sixlions.recipeweed;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewL extends Activity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview_layout);

        WebView wb = (WebView) findViewById(R.id.wv);
        wb.setWebViewClient(new myWebClient());
        wb.getSettings().setJavaScriptEnabled(true);
        int pos = getIntent().getIntExtra("key",0);
        if(pos==0){
            wb.loadUrl("file:///android_asset/608.html");
        }
        else if(pos==1){
            wb.loadUrl("file:///android_asset/609.html");
        }       

    }

    public class myWebClient extends WebViewClient
    {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub

            view.loadUrl(url);
            return true;

        }
        @Override
        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {
        }
        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);

        }
    }
}

0 个答案:

没有答案