android - 如何从android中的 ListView 按钮显示警报确认对话框

标签 android android-alertdialog custom-adapter android-viewholder

我试图在点击 ListView 中的按钮后显示确认对话框

我在 getView 方法的 CustomAdapter 中有 setOnClickListener

但是在点击监听器时出现以下错误:

02-25 21:36:32.065 20631-20631/com.themsg.chat W/Toast: From com.themsg.chat, go ahead.
02-25 21:36:32.095 20631-20631/com.themsg.chat W/System.err: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
02-25 21:36:32.095 20631-20631/com.themsg.chat W/System.err:     at android.view.ViewRootImpl.setView(ViewRootImpl.java:569)
02-25 21:36:32.095 20631-20631/com.themsg.chat W/System.err:     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:266)
02-25 21:36:32.095 20631-20631/com.themsg.chat W/System.err:     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
02-25 21:36:32.095 20631-20631/com.themsg.chat W/System.err:     at android.app.Dialog.show(Dialog.java:286)

这是我的代码:

holder.tvm.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    chatroomMembers = getItem(position);

                    Toast.makeText(getContext(), "here", Toast.LENGTH_LONG).show();
                    new AlertDialog.Builder(v.getContext())
                        .setTitle("Title")
                        .setMessage("Do you really want to whatever?")
                        .setIcon(android.R.drawable.ic_dialog_alert)
                        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int whichButton) {
                                deleteUserFromChatrrom(chatroomMembers.getId(), SessionData.getInstance().getCurrentChatroom(), position);
                            }})
                        .setNegativeButton(android.R.string.no, null).show();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

最佳答案

在 CustomAdapter 类中,您向 ListView 声明了一个变量 mContext 和一个 ArrayList 数据

    ArrayList<String> datasource;
    Context mContext;

创建一个构造函数:

    public AdapterAudio(Context mContext, ArrayList<String> data) {
    super();
    this.datasoure = data;
    this.mContext = mContext;
}

当您从 Activity 调用 CustomAdapter 时,“Activity_Main.this”就是您需要的 Context

   CustomAdapter adapter = new CustomAdapter(Activity_Main.this, listAudio_hienthi10);

现在你有了一个Context,使用声明的变量mContext来替换

    "getContext()", "v.getContext()"

现在您可以在您想要的 CustomAdapter 中单击 Button 时 Toast 或显示任何对话框。 享受你的代码吧!

关于android - 如何从android中的 ListView 按钮显示警报确认对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35632718/

相关文章:

安卓开发 : How to make a custom status bar Notification?

android - 以编程方式在堆栈小部件中显示下一个/上一个图像

android - 如何在用户长按列表项时弹出对话框确认删除?

android - ListView 行中的 TextView 在 Android 滚动时显示重复值?

javascript - 为什么使用 Android 和 JavaScript 在 AES/CBC/ZeroBytePadding 中加密会为某些字符串返回不同的结果?

java - 从 ViewPager Fragment 监听 DialogFragment dismiss 事件

android - 自定义 AlertDialog 正在占用所有屏幕

安卓:alertDialog 不工作

android - 如何从自定义 ListView 中获取数据以在 Activity 中使用?

Android clear listview链接到SQLite