android - 从自定义适配器打开对话框

标签 android

我有一个 ListView 的自定义适配器,其单元格包含一个删除按钮。 我想显示一条消息“真的删除这个条目吗?”当按下删除时。

我从自定义适配器内部处理删除按钮事件,问题是为了创建对话框我需要对当前 Activity 的引用...我该如何解决这个问题? 仅使用上下文会导致空指针异常。

最佳答案

把这段代码...

AlertDialog.Builder alertDialog = new AlertDialog.Builder((Activity) v.getContext());

            alertDialog.setTitle("Delete this item?");
            alertDialog.setMessage("Are you sure you want to delete this?");
            alertDialog.setIcon(R.drawable.icon);

            alertDialog.setPositiveButton(
                "Delete",
                new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            // Do the stuff..
                        }
                    }
                );


            }

            alertDialog.show();

在您的 ListView 点击监听器上:

@Override
    public void onListItemClick(ListView lv, View v, int position, long id) {

}

是你要找的吗?

关于android - 从自定义适配器打开对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16506929/

相关文章:

android - Android中使用AChartEngine绘制动态折线图时遇到的问题

Android Facebook 登录- 个人资料为空

java - 如何将整数变量传递给动画?

android - 限制 gridview 中的项目数

android - android中的警告对话框

AndroidX没有找到类“android.support.v4.content.ContextCompat

android - 如何在 Android 中进行后台工作时使用 AsyncTask 显示 ProgressDialog?

android - 为什么Android中的EditText是多行的

android - 收到 GCM 消息时如何启动应用程序而不是构建通知?

java - 移动 Zip 文件 (Android Studio Java)