david
david

Reputation: 518

Android - Modify last AlertDialog Title

I have two AlertDialogs and from the first AlertDialog I came to the second. My objective is to modify the Title from the first AlertDialog Using the second.

When I'm trying to set the title of the alertDialog2 using the method setTitle(), it tells me that alertDialog2 must be final, but if I set object alertDialog2 to final, I can not modify it.

AlertDialog alertDialog2 = new AlertDialog.Builder(MainActivity.this).create();
place="";
alertDialog2.setTitle(place); //this is the original place
alertDialog2.setMessage(text);

alertDialog2.setButton(Dialog.BUTTON_NEUTRAL, "Set Place", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {

        final CharSequence[] items = {" Place1 "," Place2 "," Place3 "," Place4 "};

        // Creating and Building the Dialog
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("Select Place");
        builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {


            switch(item)
            {
                case 0:
                        // Your code when first option seletced
                        place=String.valueOf(items[0]);
                         break;
                case 1:
                        // Your code when 2nd  option seletced
                    place=String.valueOf(items[1]);
                        break;
                case 2:
                       // Your code when 3rd option seletced
                    place=String.valueOf(items[2]);
                        break;
                case 3:
                         // Your code when 4th  option seletced  
                    place=String.valueOf(items[3]);
                        break;

            }

            }
        });
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                alertDialog2.setTitle(place); // Here I can not set the title
                alertDialog2.show();

            }
        });
      builder.create();
      builder.show();
    }
});
alertDialog2.setButton(Dialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        Toast.makeText(MainActivity.this, "Canceled", Toast.LENGTH_SHORT).show();

    }
});

alertDialog2.show();

Upvotes: 0

Views: 82

Answers (2)

Ahmad
Ahmad

Reputation: 437

Try once by making AlertDialog alertDialog2 as class variable instead of defining and intializing in any function.

AlertDialog alertDialog2;

void tempFunction()
{
       alertDialog2=new AlertDialog.Builder(MainActivity.this).create();
       .
       .
       .
       .

}

Hope it helps...

To understand this kind of thing give 2 mins to understand the memory map and stacking whenever a function executes its local resources are added to stack and executed so if u are assigning a dialog in local function it will be removed as soon as it leaves from there... but if u assign it as global and suppose it has address of 2100 then u can change the content everytime its like a pot which u can fill with watever type of water like salty , sweet etc... memory maps and imagination do help alot and will help to also overcome Nullpointer exceptions at many cases...

thx

Upvotes: 1

ashishduh
ashishduh

Reputation: 6699

You can just declare it to be final as long as you aren't reassigning to it later in your code.

http://en.wikipedia.org/wiki/Final_(Java)

Also, your AlertDialog doesn't have a title when created. You need to give it a title when you create it, you can't just add the title once it's created.

Upvotes: 0

Related Questions