gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 20:06:16 24 4
gpt4 key购买 nike

我试图在点击 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com