nexus490
nexus490

Reputation: 817

Which context do i need?

I'm creating a dialog box and using the (this) isnt working. Up until now its just been a button calling a dialogbox but now the button within the called dialogbox needs to call another dialog. The Dialog dialogdelcon is the one with problem.

Here is the code:

case R.id.delappt:
        //rmvall();

        final Dialog dialogdelsel = new Dialog(this);
           dialogdelsel.setContentView(R.layout.delsel);
           dialogdelsel.setTitle("What would you like to do?");
           dialogdelsel.setCancelable(true);

          Button btndelsel = (Button) dialogdelsel.findViewById(R.id.btndelsel);
          btndelsel.setOnClickListener(new OnClickListener() {


               @Override
               public void onClick(View v) {
                   // delete selected code here.

               }
           });

         Button btndelall = (Button) dialogdelsel.findViewById(R.id.btndelall);
          btndelall.setOnClickListener(new OnClickListener() {


               @Override
               public void onClick(View v) {
                   // delete all code here.
                  final Dialog dialogdelcon = new Dialog();
                   dialogdelcon.setContentView(R.layout.delcon);
                   dialogdelcon.setTitle("Deletion Confirmation");
                   dialogdelcon.setCancelable(true);

                  Button buttoncnclok = (Button) dialogdelcon.findViewById(R.id.btndelcon);
                   buttoncnclok.setOnClickListener(new OnClickListener() {

                    // on click for cancel button
                       @Override
                       public void onClick(View v) {
                           dialogdelcon.dismiss();
                       }
                   });

                 dialogdelcon.show();
               }
           });


        dialogdelsel.show();
        break;

Upvotes: 0

Views: 117

Answers (3)

drasticp
drasticp

Reputation: 317

To improve the isolation between the two dialogs, it would be best to call showDialog(R.id.delapptcon) from the onClick handler. Then load the new dialog in the onCreateDialog of your activity. In this way, you can create more reusable dialogs and avoid the scoping issue you have now.

Upvotes: 0

bos
bos

Reputation: 6535

If this code is in the onCreate() method, or similiar, add getApplicationContext() instead of this and you should be fine. That's because this in a Button-context will refer to the button environment.

Upvotes: 0

Samir Mangroliya
Samir Mangroliya

Reputation: 40406

getApplicationContext() or use YourActictyName.this Because this refers the button click listner ,not your class Object

Upvotes: 1

Related Questions