当我点击不显示alertdialog的项目时?

时间:2011-10-19 13:04:29

标签: android

我想在点击pdfimage时显示警告对话,我尝试按照代码但不显示alertdialogue。

private OnItemClickListener itemClickListener=new OnItemClickListener() {
  @SuppressWarnings("rawtypes")
  public void onItemClick(AdapterView parent, View arg1, int position, long arg3) {
      int i=position;
      pdf=pdfarray[i];
 /*******************************/
  AlertDialog.Builder builder = new AlertDialog.Builder(ImageShowActivity.this);
    final AlertDialog alert = builder.create();
    builder.setMessage("Are you sure you want to exit?")
       .setNeutralButton("Cancel",new DialogInterface.OnClickListener(){
       public void onClick(DialogInterface dialog, int id) {
            alert.dismiss();   
           }
        })
       .setPositiveButton("Download", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
           Intent intent=new Intent(ImageShowActivity.this,OpenPDFNew.class);
           intent.putExtra("pdfurl",pdf );
           startActivity(intent);
           }
       })
      .setNegativeButton("Online", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
           }
   });
    alert.show();
/****************************************/
 }
};

2 个答案:

答案 0 :(得分:1)

您在声明alert Builder的内容之前正在调用builder.create(),您应该在创建此类内容后调用builder.create()

AlertDialog.Builder builder = new AlertDialog.Builder(Activity_name.this);
        builder.setItems(items, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int position) {
                .........
                }
            }
        });
        builder.create().show();

答案 1 :(得分:0)

您应该更改代码并删除final AlertDialog alert = builder.create();

也在alert.show();

替换为

builder.create().show();