在另一个对话框的单击事件中创建对话框时出现问题

时间:2011-07-14 05:51:45

标签: java android alertdialog

我喜欢在另一个对话框的click事件中显示一个对话框。我的代码如下所示。但它显示错误 - 令牌上的语法错误“)”,;预期,令牌上的语法错误“)”,;期待。任何人都可以解决这个问题

  

final CharSequence [] PhoneModels = {“Pub Date”,“Catagory”,   “量”};             final AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);                     alt_bld.setTitle(“选择一个选项”);

      alt_bld.setSingleChoiceItems(PhoneModels, -1, new DialogInterface.OnClickListener() {

      public void onClick(DialogInterface dialog, int item) {

      //UpdateDisplay();
          //   dialog.dismiss();

          getApplicationContext();
          if(PhoneModels[item]=="Pub Date")
               {@Override
              protected Dialog onCreateDialog(int id)
               {
                   Calendar c = Calendar.getInstance();
                   int cyear = c.get(Calendar.YEAR);
                   int cmonth = c.get(Calendar.MONTH);
                   int cday = c.get(Calendar.DAY_OF_MONTH);
                   switch (id) {
                   case DATE_DIALOG_ID:
                   return new DatePickerDialog(this,  mDateSetListener,  cyear, cmonth, cday);
                   }
                   return null;
                   }
               private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
                  // onDateSet method
                  public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                  String date_selected = String.valueOf(monthOfYear+1)+" /"+String.valueOf(dayOfMonth)+" /"+String.valueOf(year);
                  Toast.makeText(ExampleApp.this, "Selected Date is ="+date_selected, Toast.LENGTH_SHORT).show();
                  }
                  };

              Toast.makeText(getApplicationContext(), " will be here", Toast.LENGTH_SHORT).show();
                 }
          else if(PhoneModels[item]=="Catagory")
                     {Toast.makeText(getApplicationContext(), " will not be here", Toast.LENGTH_SHORT).show();
                        }
          else
                       {Toast.makeText(getApplicationContext(), "It will be here", Toast.LENGTH_SHORT).show();
                          }
      }
      });

      AlertDialog alert = alt_bld.create();
      alert.show();

    // display UI

}

2 个答案:

答案 0 :(得分:1)

如果您显示错误发生的 (并正确缩进代码),这确实会有所帮助,但这肯定是一个你的问题:

if(PhoneModels[item]=="Pub Date")
{@Override
    protected Dialog onCreateDialog(int id)

你不能在if体内声明一种方法,有条件地覆盖。

您似乎也试图在方法中声明一个私有变量,该变量也无效。

很难准确理解你的代码应该做什么,说实话 - 在另一个内部创建一个匿名内部类真的没有帮助。你能否将你的匿名内部类提取到“普通”类中(如果有帮助的话,可能还是内部类)?

另外请注意,在Java中使用==比较字符串几乎总是不正确的。它将比较引用,而不是检查字符串是否相等。

答案 1 :(得分:0)

您正在尝试在块中定义方法。这在Java中是不可能的。

请参阅:

if(PhoneModels[item]=="Pub Date")
{
  @Override
  protected Dialog onCreateDialog ... // this is not allowed!