1

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.

4

4 回答 4

5

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

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

于 2012-06-14T08:16:20.010 回答
2

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

于 2012-06-14T08:16:39.620 回答
2

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();
于 2012-06-14T08:20:15.780 回答
1

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

于 2012-06-14T08:18:34.753 回答