简体   繁体   中英

android - How to display the previous status if you click on the cancel button of alert dialog multi choice items

In my application I am using alert dialog consists of multi choice items with positive and negative buttons. If you change the states of choice items, suppose if you click on ok button then next time dialog opens I want to show the update status. if you click on the cancel button then next time I want to show the previous status.Ok button is working fine but cancel button functionality is not working I tried but didn't get please can anybody help me.

Code:

protected Dialog onCreateDialog(int id) 
{

    switch (id) { 
    case DIALOG_MULTI_CHOICE:
         System.out.println("In dialog Choice_Checked:"+Arrays.toString(choice_checked));       
        return new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.singlechoicelistitem))
            .setTitle("Bookmarking for")
            .setMultiChoiceItems(choice, choice_checked, new OnMultiChoiceClickListener() {
                public void onClick(DialogInterface dialog, int index, boolean status) {                                      
                    if(status)
                    {
                        choice_checked_dynamic[index] = true;
                    }    
                    else
                    {
                        choice_checked_dynamic[index] = false;
                    }
                }                   
            })
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {                       
                    saveBookmarkAction();
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                      ((Builder) dialog).setMultiChoiceItems(choice, choice_checked, null);
                      ((Builder) dialog).create();                                          
                }
            })
           .create();
    }
    return null;
}

如果您以前的状态是活动,则在onClick的取消按钮上,意图使用先前的活动,否则仅将onClick函数保留为空白将起到保持相同活动的作用。

place dialog.cancel in negative button code

.setNegativeButton("No", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
               }
           });

on click on cancel button you need to save the default settings.

create a method to set default setting and call that method onclick of cancel button

Sample Code

.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                       saveDefaultSettings(); //this method sets the default settings of tha app                                    
                }
            })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM