[英]How can I use AlertDialog in an Adapter class?
I have send the Context to the Adapter 我已将上下文发送到适配器
public ImageAdapter(Context context, List<Image> imagesList) {
this.context = context;
mImageList = imagesList;
}
And I use AlertDialog this way: 我以这种方式使用AlertDialog:
AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.AlertDialogCustom);
But I got error: 但是我得到了错误:
Unable to add window -- token null is not valid; is your activity running?
从Activity / fragment相应地将上下文作为ActivityName.this / getActivity()发送到调用适配器的位置
To do that you need to define call back. 为此,您需要定义回叫。
define object as parameter for your adapter like 将对象定义为适配器的参数,例如
public ImageAdapter(Context context, List imagesList, Callback callback) { this.context = context; public ImageAdapter(Context context,List imagesList,Callback callback){this.context = context; mImageList = imagesList;
mImageList = imagesList; this.callback =callback;
this.callback =回调 }
}
inside your adapter call method inside your call back as you want your activity will listen to this action and inside this method in your activity you can create your alert dialog 在适配器调用方法内部,在您希望的活动中回叫,您的活动将监听此操作,在活动中的此方法内部,您可以创建警报对话框
您应该将Activity而不是Context传递给AlertDialog.Builder。的构造函数,并且必须确保在对话框运行时该活动正在运行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.