This code is used to display a alert box for confirming the data as well as saving the data in the database(SQLite) in Android application.Here we are having the two options as "Ok" and "Cancel" and using the methods as "PositiveButton(OK)" and "NegativeButton(Cancel)".Based up on this,data will be updated/saved in the database.
public class SampleDemoActivity { final private String CLASS_NAME = "SampleDemoActivity"; protected Button saveButton,clearButton; protected AlertDialog saveConfirmDialog, protected AlertDialog.Builder saveConfirmBuilder; @Override public void onCreate(Bundle savedInstanceState) { //Initialize Activity super.onCreate(savedInstanceState); } public void setButtonListeners() { OnClickListener saveButtonClickListener = new OnClickListener() { @Override public void onClick(View v) { /*set the functionality for the cancel button*/ saveConfirmBuilder.setNegativeButton("NO",new DialogInterface.OnClickListener(){ @Override public void onCLick(DialogInterface dialog, int which) { //Cancel button clicked;data will not be updated; } }); /*set the fucntionality for the OK button*/ saveConfirmBuilder.setPositiveButton("YES",new DialogInterface.OnClickListener(){ @Override public void onCLick(DialogInterface dialog, int which) { saveData(); //Data Saved; } }); /*Create and display the call confirm dialog for the builder*/ saveConfirmDialog = saveCOnfirmBuilder.create(); saveConfirmDialog.show(): } };