WebView无法从ListView按钮

时间:2016-11-16 14:51:28

标签: java android listview webview

所以我想要一个包含大量项目的ListView,列表中的每个项目都包含一个按钮,打开一个打开不同URL的webview,但它没有加载正确的URL。使用自定义适配器我设置URL和描述。我将说明设置为与网址相同,并且说明的网址是准确的,但是网页视图加载了一个不同的网站,并且它看起来并不一致,似乎每次我启动时它都会加载不同的网址活动

这里是MainActivity.java

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    deities deity_data[] = new deities[]
            {
                    new deities(R.drawable.transparent, "Test 1", "http://google.com", 1, Color.parseColor("#ff3399")),
                    new deities(R.drawable.transparent, "Test 2", "http://facebook.com", 1, Color.parseColor("#9933ff")),
                    new deities(R.drawable.transparent, "Test 3", "http://youtube.com", 1, Color.parseColor("#0099ff")),
                    new deities(R.drawable.transparent, "Test 4", "http://pinterest.com", 1, Color.parseColor("#009900")),
                    new deities(R.drawable.transparent, "Test 5", "http://twitter.com", 1, Color.parseColor("#cc0000")),
                    new deities(R.drawable.transparent, "Test 6", "http://facepunch.com", 1, Color.parseColor("#0000cc")),
                    new deities(R.drawable.transparent, "Test 7", "http://ifttt.com", 1, Color.parseColor("#333300"))

            };
    deityListAdapter adapter = new deityListAdapter(this,
            R.layout.deity_list_item, deity_data);

    ListView deityList = (ListView)findViewById(R.id.deityList);
    deityList.setAdapter(adapter);



}

dietyListAdapter.java:

public class deityListAdapter extends ArrayAdapter<deities> {

Context context;
int layoutResourceId;
deities data[] = null;
deities deity;

public deityListAdapter(Context context, int layoutResourceId, deities data[]) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent){
    View ListItem = convertView;
    Holder holder;

    if (ListItem == null) {

        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        ListItem = inflater.inflate(layoutResourceId, parent, false);

        holder = new Holder();
        holder.description = (TextView)ListItem.findViewById(R.id.deityDesc);
        holder.banner = (ImageView)ListItem.findViewById(R.id.deityBanner);
        holder.website = (Button)ListItem.findViewById(R.id.deityWebsite);
        ListItem.setTag(holder);

    } else {
        holder = (Holder) ListItem.getTag();
    }
    deity = data[position];
    View.OnClickListener holderListener = new View.OnClickListener() {
        public void onClick(View v) {
            WebView webView = (WebView)((Activity) context).findViewById(R.id.webView);

            webView.setWebChromeClient(new WebChromeClient() {
                public void onProgressChanged(WebView view, int progress) {
                    ProgressBar webViewProgressBar = (ProgressBar) ((Activity) context).findViewById(R.id.webViewProgress);
                    webViewProgressBar.setVisibility(View.VISIBLE);
                    webViewProgressBar.setProgress(progress);

                    if (progress == 100) {
                        webViewProgressBar.setVisibility(View.GONE);
                    }

                }
            });
            webView.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url)
                {
                    return false;
                }
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
                {
                    return false;
                }
            });
            webView.getSettings().setJavaScriptEnabled(true);
            webView.setVisibility(View.VISIBLE);
            webView.loadUrl(deity.url);





        }
    };


    holder.website.setOnClickListener(holderListener);
    holder.banner.setImageResource(deity.banner);
    holder.description.setText(deity.description);
    holder.banner.setBackgroundColor(deity.color);
    holder.description.setText(deity.url);
    return ListItem;

}
static class Holder {

    TextView description;
    ImageView banner;
    Button website;



}

dieties.java:

public class deities {

public int banner;
public String description;
public String url;
public int color;
public int id;
public deities(){
    super();
}

public deities(int banner, String description, String url, int id, int color){
    super();
    this.banner = banner;
    this.description = description;
    this.url = url;
    this.color = color;
    this.id = id;
}
}

1 个答案:

答案 0 :(得分:1)

deity = data[position];替换为deities deity = data[position]; 使用类似现在的代码,您的网址始终是最后呈现项目的网址。

还有一件事,这个使用WebView的代码应该在Activity中。不要将整个Activity传递给Adapter。创建适配器将与Activity通信的接口,在Activity中实现它并在Adapter中作为该接口传递Activity。比onClick,只需在界面中调用一个方法,并在Activity中完成有关WebView的工作。