在一个view类中需要弹出一个对话框,使用了:new AlertDialog.Builder(MainActivity.this)
但是Builder()括号中需要的是activity.本人菜鸟,请各位高手指点
另外是不是需要在activity定义好一个activity然后进行调用,本人试了一下,在activity类中写了一个静态函数:
public static void onClickTest() {
// TODO Auto-generated method stub
new AlertDialog.Builder(MainActivity.instance)
.setTitle(“TITLE”)
.setMessage(“CONTENT”)
.setPositiveButton(“YES”,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.setNegativeButton(“NO”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.show();
}
然后在view中通过MainActivity.onClickTest();进行调用,但是调用的时候还是没有反应。
求指导!多谢!
但是Builder()括号中需要的是activity.本人菜鸟,请各位高手指点
另外是不是需要在activity定义好一个activity然后进行调用,本人试了一下,在activity类中写了一个静态函数:
public static void onClickTest() {
// TODO Auto-generated method stub
new AlertDialog.Builder(MainActivity.instance)
.setTitle(“TITLE”)
.setMessage(“CONTENT”)
.setPositiveButton(“YES”,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.setNegativeButton(“NO”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
.show();
}
然后在view中通过MainActivity.onClickTest();进行调用,但是调用的时候还是没有反应。
求指导!多谢!
解决方案
4
给你本人写的
AlertDialog.Builder builder = new Builder(CramerProActivity.this); builder.setMessage("确认移除已添加图片吗?"); builder.setTitle("提示"); builder.setPositiveButton("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); imageItem.remove(position); fileNames.remove(position); imagePath.remove(position); simpleAdapter.notifyDataSetChanged(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show();
4
什么叫独立的view类?你这个view类不需要Context 吗?有context不就好了
4
getApplication()
4
你可以在调用这个view的时候把Context传过来,然后在view里new Builder(context)
4
AlertDialog.Builder builder = new Builder(getContext()); builder.setMessage("确认移除已添加图片吗?"); builder.setTitle("提示"); builder.setPositiveButton("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); imageItem.remove(position); fileNames.remove(position); imagePath.remove(position); simpleAdapter.notifyDataSetChanged(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show();
把上面的AlertDialog.Builder builder = new Builder(CramerProActivity.this);改为AlertDialog.Builder builder = new Builder(getContext());