user47
user47

Reputation: 415

AlertDialog not working

Guys I have an activity which has a home button, when the button is pressed,an alert dialog should appear with the message "Exit without Saving ?" and the following options(buttons) should be available to the user:

1-> Yes

2->No

3->Save and Exit

But the problem is when the home button is pressed NO alert dialog is being displayed.

I tried the following code:

// this is when the button is pressed

public void onClick(View v) {
    // TODO Auto-generated method stub
switch (v.getId()) {

case R.id.backHome:
        final AlertDialog alertDialog = new AlertDialog.Builder(
                DataView.this).create();
        // alertDialog.setTitle("Exit Without Save ?");
        alertDialog.setMessage("Exit Without Saving");
        alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Intent i = new Intent(DataView.this, DiaryActivity.class);
                startActivity(i);
                finish();

            }
        });

        alertDialog.setButton2("No", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                alertDialog.dismiss();

            }
        });
        alertDialog.setButton3("Save and Exit",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        rowIdreceived = getdata.getLong("row_id");
                        String title_updated = topicDisplay.getText()
                                .toString() + " ";
                        String story_updated = StoryField.getText()
                                .toString();
                        DataHolder entry = new DataHolder(DataView.this);

                        try {
                            entry.open();
                        } catch (SQLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        entry.updateEntry(rowIdreceived, title_updated,
                                story_updated);

                        entry.close();
                        Intent i = new Intent(DataView.this,
                                DiaryActivity.class);
                        startActivity(i);
                        finish();

                    }
                });


        break;

The DiaryActivity is the main class.

DataView is the current activity.

The Yes button simply exits out of the current activity and goes back the main activity that is the DiaryActivity.

The No button "dismisses" the alert dialog and the user can then save his work and exit later.

The save and exit button save the work into the database and then exits out of the current activity to the main activity.

Upvotes: 1

Views: 1960

Answers (4)

Dheeresh Singh
Dheeresh Singh

Reputation: 15701

alertDialog.show(); is missing in the code......... you have created the dialog but not shown that ....

put alertDialog.show() just before break .....

Upvotes: 5

Ravi1187342
Ravi1187342

Reputation: 1277

theck out this. call alertDialog.show(); to show alertdialog

AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Reset...");
alertDialog.setMessage("Are you sure?");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int which) {
      // here you can add functions
   }
});
alertDialog.setIcon(R.drawable.icon);
alertDialog.show();

Upvotes: 2

Riskhan
Riskhan

Reputation: 4472

please call alertDialog.show(); method to display alert dialog

Upvotes: 1

Bhavin
Bhavin

Reputation: 6010

You have forgot to write alertDialog .show();, it should be after creation of the alertDialog.

Upvotes: 2

Related Questions