1

I like to display one dialog box in the click event of another dialog box.My code is given below.But it shows an error- Syntax error on token ")", ; expected,Syntax error on token ")", ; expected.Can anybody clear this problem

final CharSequence[] PhoneModels = {"Pub Date", "Catagory", "Amount"}; final AlertDialog.Builder alt_bld = new AlertDialog.Builder(this); alt_bld.setTitle("Select An Option");

      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

}
bejoy george
  • 29,385
  • 5
  • 19
  • 15

2 Answers2

1

It would really have helped if you'd shown where the error occurred (and indented the code properly) but this certainly looks like one of your problems:

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

You can't declare a method within an if body, sort of conditionally overriding.

You also seem to be attempting to declare a private variable within a method, which isn't valid either.

It's hard to understand exactly what your code is meant to do, to be honest - and creating one anonymous inner class within another really isn't helping on that front. Can you extract your anonymous inner classes into "normal" classes (possibly still inner classes, if that helps)?

Additionally note that comparing strings using == in Java is almost always incorrect. It will be comparing the references rather than checking whether the strings are equal.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • You are absolutely right,that was my error in code.But is there any other way to display date picker alert dialog in the click event of another alert dialog? – bejoy george Jul 14 '11 at 06:21
  • @bejoy: Any time you're using anonymous inner classes, there's an alternative. It's not really clear what you're trying to do, or why you've got invalid Java as an attempt to achieve it... – Jon Skeet Jul 14 '11 at 06:24
  • I am generating one alert dialog with 3 radiobuttons.If the user selects pubdate ,i want display a date picker dialog and according to that date i want to do another process. – bejoy george Jul 14 '11 at 06:40
  • @bejoy: Right, so you need to take a new action in certain situations - but that doesn't mean conditionally overriding a method. It means *executing some code*. – Jon Skeet Jul 14 '11 at 07:30
  • how can i perform this task.And what is the changes i have to make in this code. – bejoy george Jul 14 '11 at 07:36
  • @bejoy: I'm not an android developer, so I don't know the specifics - but I would *start* by trying to clean up you're existing code so it's better structured. Strongly consider using normal named classes for anything longer than a few lines of code - I'm sure that part of the problem is that it's hard to tell what's going on with the level of nesting you've got. – Jon Skeet Jul 14 '11 at 07:40
  • Actually i didnt write the full code.Iam just trying with showing messages.I am very much new to android and java too. – bejoy george Jul 14 '11 at 07:52
  • @bejoy: I would strongly encourage you to read books/tutorials on "plain" Java, leaving the Android part alone to start with. It's very hard to try to do two new things at the same time - and you'll be able to experiment rather more easily with Java console apps than Android applications. (This isn't specific to Android - I give the same advice whenever people are trying to learn a language *and* a specific high-level technology at the same time.) – Jon Skeet Jul 14 '11 at 07:55
0

You are trying to define a method within a block. That's not possible in Java.

See:

if(PhoneModels[item]=="Pub Date")
{
  @Override
  protected Dialog onCreateDialog ... // this is not allowed!
powerMicha
  • 2,753
  • 1
  • 24
  • 31
  • 1
    @Eng.Fouad: Because it's not an acronym. There is absolutely no reason to put it all in capitals, and it just looks shouty. – Jon Skeet Jul 14 '11 at 06:13