在自定义 ListAdapter 类中显示 AlertDialog [英] Displaying AlertDialog inside a Custom ListAdapter Class

查看:30
本文介绍了在自定义 ListAdapter 类中显示 AlertDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难在扩展 BaseAdapter 的自定义 ListView 类中显示 AlertDialog.

I am having a hard time dealing with displaying a AlertDialog inside a Custom ListView class which extends a BaseAdapter.

AlertDialog.Builder alertbox = new AlertDialog.Builder(getParent().getApplicationContext());
        alertbox.setMessage("No Internet Connection");
        alertbox.setTitle("Warning");
        alertbox.setIcon(R.drawable.trn_03);

        alertbox.setNeutralButton("OK",
                new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface arg0,
                            int arg1) {

                    }
                });
  alertbox.show();

以上是我使用的代码,LogCat的错误是,

The above is the code I am using, and the LogCat error is,

06-16 11:33:25.686: ERROR/AndroidRuntime(690): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

我认为问题出在上下文上.我尝试了一些替代方案.但没有一个有效.任何人都可以帮助我吗?

I believe that the problem is because of the context. I tried a few alternative. But none works. Can anyone help me in this?.

推荐答案

对上下文的轻微修改对我来说是一个技巧.这是编辑后的片段.

A slight modification with the context did teh trick for me. Here is the edited snippet.

AlertDialog.Builder alertbox = new AlertDialog.Builder(v.getRootView().getContext());
    alertbox.setMessage("No Internet Connection");
    alertbox.setTitle("Warning");
    alertbox.setIcon(R.drawable.trn_03);

    alertbox.setNeutralButton("OK",
            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface arg0,
                        int arg1) {

                }
            });
  alertbox.show();

这篇关于在自定义 ListAdapter 类中显示 AlertDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆